问题
I can't get Entity Framework to use the Oracle Provider (ODP.NET) with my project.
Setup:
- ASP.NET Core MVC 2.1 targeting .NET Framework 4.7.2
- EntityFramework 6.2
- ODP.NET 18.3 (Oracle.ManagedDataAccess and Oracle.ManagedDataAccess.EntityFramework)
Although I'd prefer to use EF Core, I can't because Oracle isn't supporting EF Core yet, just .NET Core.
The errors I'm receiving indicate that the application is trying to use the SQL Server driver.
I can't find an example online for my scenario. Either its MVC5 with EF6/ODP.NET, or .NET Core examples with Oracle that don't have EF.
My assumption is the problem lies in that in MVC5 configures it through web.config/app.config. I'm assuming I need to configure Oracle in start.cs but need the right syntax.
What I have coded for the Context class:
public class MainContext : DbContext
{
public MainContext(string connectionString) : base(connectionString)
{
Database.SetInitializer<MainContext>(null);
}
public virtual DbSet<ApplicationSetting> ApplicationSettings { get; set; }
}
Then I created a factory:
public class MainContextFactory : IDbContextFactory<MainContext>
{
private readonly string _connectionString;
public MainContextFactory(string connectionString)
{
_connectionString = connectionString;
}
public MainContext Create()
{
return new MainContext(_connectionString);
}
}
In Startup.cs I have:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
services.AddTransient<IDbContextFactory<MainContext>>(d =>
new MainContextFactory(Configuration["ConnectionStrings:Primary"]));
I call this from my Repository project (targets .NET 4.7.2) and contains the MainContext:
public class ApplicationSettingRepository : BaseDbRepository, IApplicationSettingRepository
{
private readonly ILogger<ApplicationSettingRepository> _logger;
public ApplicationSettingRepository(ILogger<ApplicationSettingRepository> logger,
IUserContext userContext,
IDbContextFactory<MainContext> dbContextFactory) : base(userContext, dbContextFactory)
{
_logger = logger;
}
/// <summary>
/// Get All Application Settings
/// </summary>
public async Task<List<IApplicationSetting>> GetAllAsync()
{
var list = new List<IApplicationSetting>();
using (var db = _contextFactory.Create())
{
list.AddRange(await db.ApplicationSettings.ToListAsync());
}
return list;
}
which calls a base repository class:
public abstract class BaseDbRepository : IBaseRepository
{
protected IDbContextFactory<MainContext> _contextFactory;
public IUserContext UserContext { get; set; }
protected BaseDbRepository(IUserContext userContext, IDbContextFactory<MainContext> dbContextFactory)
{
UserContext = userContext;
_contextFactory = dbContextFactory;
}
}
Questions:
- What do I need to update or add to make it call the ODP.NET provider?
- Is there a better approach to config?
回答1:
To associate the Oracle Provider:
Update add.config with the values that were in web.config from MVC5:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</configSections>
<runtime>
<gcServer enabled="true"/>
</runtime>
<entityFramework>
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client" />
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources></dataSources>
</version>
</oracle.manageddataaccess.client>
</configuration>
Then add after the services.AddMvc() in startup.cs:
services.AddScoped(provider =>
{
return new OracleDbContext(Configuration["ConnectionString"]);
});
Credit to Tony Sneed Post.
来源:https://stackoverflow.com/questions/52786319/odp-net-and-ef6-configuration-on-asp-net-core-targeting-net-4-7-2