Oracle database connection for Entity Framework

c# entity-framework-6 identity oracle


I'm trying to make a token-based authorization in my Web API service, planning to store identity tables in my Oracle database.

I know there are two ways of accessing the database - via Oracle ManagedDataAccess or Oracle DataAccess. I've downloaded managed version for Entity Framework 6.

Here is the part of my web config:

(i've modified credentials and string path, just assume that they are correct and i CAN connect WITHOUT Entity Framework - but only with unmanaged DataAccess)

    <version number="*">
            <dataSource alias="Test" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)))" />
    <add name="Test" 
         providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=login;Password=pass;Data Source=Test" />
    <clear />
    <add name="OraAspNetConString" connectionString=" " />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
            <parameter value="v13.0" />
        <provider invariantName="Oracle.ManagedDataAccess.Client" 
                  type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=, Culture=neutral, PublicKeyToken=89b483f429c47342" />

And that's the part of dbContext - OnModelCreating method should force identity to use my selfprepared tables (or I hope it would, haven't got a chance to test them):

 public class AuthContext : IdentityDbContext<IdentityUser>
     public AuthContext() : base("Test")

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
         base.OnModelCreating(modelBuilder); // MUST go first.

         modelBuilder.HasDefaultSchema("MY_SCHEME"); // Use uppercase!


    public static AuthContext Create()
        return new AuthContext();

So... when I'm using connection string with ManagedDataAccess:

<add name="Test" 
     connectionString="User Id=login;Password=pass;Data Source=Test" />

I'm getting the error :

ORA-12154: TNS:could not resolve the connect identifier specified

When I'm turning to unmanaged version like this :

<add name="Test" 
     connectionString="User Id=login;Password=pass;Data Source=Test" />

I'm getting an error that Entity Framework couldn't find any dbContext named Oracle.DataAccess.Client.

In my other projects I'm using Oracle.ManagedDataAccess.Client, but typing Oracle.DataAccess.Client in my connection string - and it works just fine!

The simple question - how do I connect to Oracle with EF6? Via managed and unmanaged data access? (because there is no nuget for the unmanaged framework version)

2/2/2017 1:25:53 PM

Popular Answer

I don't have a clear answer for you but following may help to find the root cause.

Oracle.ManagedDataAccess.Client and Oracle.DataAccess.Client use different methods to resolve TNS aliases, I assume there is the problem.

Oracle.ManagedDataAccess.Client checks following:

  1. data source alias in the dataSources section under <oracle.manageddataaccess.client> section in the .NET config file (i.e. machine.config, web.config, user.config).
  2. data source alias in the tnsnames.ora file at the location specified by TNS_ADMIN in the .NET config file. Locations can consist of either absolute or relative directory paths.
  3. data source alias in the tnsnames.ora file present in the same directory as the .exe.

whereas Oracle.DataAccess.Client is looking for tnsnames.ora file in these locations:

  1. Environment variable TNS_ADMIN
  2. Registry value HKLM\SOFTWARE\ORACLE\KEY_{Oracle_Home_Name}\TNS_ADMIN, resp. HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_{Oracle_Home_Name}\TNS_ADMIN
  3. Folder %ORACLE_HOME%\network\admin
  4. Current directory (which can be different to directory where your application is located)
  5. Folder where your application is located
2/2/2017 1:53:14 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow