You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Entity Framework的C# Oracle应用遇ORA-12154错误求助

Fixing ORA-12154 Error in EF C# App Connecting to Oracle

Hey there, let's work through this ORA-12154 error you're facing with your Entity Framework C# app that connects to two Oracle databases. This error boils down to the Oracle driver being unable to resolve the TNS alias in your connection string, so let's break down actionable steps to fix it:

  • First, verify the TNS alias is resolvable on the test server
    Grab a command prompt or PowerShell window on the test server and run:

    tnsping YourTnsAliasName
    

    Replace YourTnsAliasName with the alias used in your EF connection string (do this for both of your Oracle databases!). If tnsping returns an error like "TNS could not resolve", you know the core issue is TNS configuration—move to the next step.

  • Check the tnsnames.ora file on the test server
    Oracle Managed Data Access looks for tnsnames.ora in these locations (in order):

    1. The directory where your application is running
    2. The %ORACLE_HOME%\network\admin folder (if an Oracle client is installed on the server)
    3. The directory specified by the TNS_ADMIN environment variable
      Open the file and confirm your TNS alias entries are correct for the test environment. Each entry should look something like this:
    TEST_DB_1 =
      (DESCRIPTION =
        (ADDRESS_LIST =
          (ADDRESS = (PROTOCOL = TCP)(HOST = test-oracle-server-1)(PORT = 1521))
        )
        (CONNECT_DATA =
          (SERVICE_NAME = test-db-service-1)
        )
      )
    
    TEST_DB_2 =
      (DESCRIPTION =
        (ADDRESS_LIST =
          (ADDRESS = (PROTOCOL = TCP)(HOST = test-oracle-server-2)(PORT = 1521))
        )
        (CONNECT_DATA =
          (SERVICE_NAME = test-db-service-2)
        )
      )
    

    Double-check the HOST, PORT, and SERVICE_NAME values—typos here are the #1 cause of this error.

  • Explicitly set TNS_ADMIN in your app config
    If the test server doesn't have a TNS_ADMIN environment variable set, or you want to ensure your app uses a specific tnsnames.ora file, add this configuration to your app.config or web.config:

    <configuration>
      <oracle.manageddataaccess.client>
        <version number="*">
          <settings>
            <setting name="TNS_ADMIN" value="C:\Path\To\Your\Tns\Folder"/>
          </settings>
        </version>
      </oracle.manageddataaccess.client>
    </configuration>
    

    This tells the Oracle driver exactly where to find the TNS configuration file.

  • Test with an EZConnect connection string
    To rule out TNS alias issues entirely, temporarily replace your connection string with an EZConnect format (for both databases):

    Data Source=test-oracle-server-ip:1521/test-db-service-name;User Id=YourUsername;Password=YourPassword;
    

    If this works, you can confirm the problem is definitely with TNS alias resolution—go back and fix your tnsnames.ora or TNS_ADMIN setup.

  • Check network connectivity to the Oracle servers
    If tnsping fails even with correct TNS settings, test if the test server can reach the Oracle database ports:

    Test-NetConnection test-oracle-server-ip -Port 1521
    

    If this returns a failure, you're dealing with a network firewall rule or database server accessibility issue—reach out to your DBA or IT team to resolve this.

  • Confirm Oracle Managed Data Access version consistency
    Make sure the version of Oracle.ManagedDataAccess.dll you deployed to the test server matches the version you used during development. Mismatched versions can sometimes cause unexpected TNS resolution bugs.

内容的提问来源于stack exchange,提问作者Adel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:52:47