使用Entity Framework的C# Oracle应用遇ORA-12154错误求助
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 YourTnsAliasNameReplace
YourTnsAliasNamewith 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.orafile on the test server
Oracle Managed Data Access looks fortnsnames.orain these locations (in order):- The directory where your application is running
- The
%ORACLE_HOME%\network\adminfolder (if an Oracle client is installed on the server) - The directory specified by the
TNS_ADMINenvironment 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, andSERVICE_NAMEvalues—typos here are the #1 cause of this error.Explicitly set
TNS_ADMINin your app config
If the test server doesn't have aTNS_ADMINenvironment variable set, or you want to ensure your app uses a specifictnsnames.orafile, add this configuration to yourapp.configorweb.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.oraorTNS_ADMINsetup.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 1521If 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 ofOracle.ManagedDataAccess.dllyou deployed to the test server matches the version you used during development. Mismatched versions can sometimes cause unexpected TNS resolution bugs.
内容的提问来源于stack exchange,提问作者Adel

