我无法使Entity Framework在我的项目中使用Oracle Provider(ODP.NET)。
设置:
尽管我更喜欢使用EF Core,但我不能这样做,因为Oracle尚不支持EF Core,仅支持.NET Core。
我收到的错误表明该应用程序正在尝试使用SQL Server驱动程序。
我无法在线找到适合我的情况的示例。带有EF6 / ODP.NET的MVC5,或带有EF的Oracle的.NET Core示例。
我的假设是问题在于MVC5中通过web.config / app.config对其进行配置。我假设我需要在start.cs中配置Oracle,但是需要正确的语法。
我为Context类编写的代码:
public class MainContext : DbContext
{
public MainContext(string connectionString) : base(connectionString)
{
Database.SetInitializer<MainContext>(null);
}
public virtual DbSet<ApplicationSetting> ApplicationSettings { get; set; }
}
然后我创建了一个工厂:
public class MainContextFactory : IDbContextFactory<MainContext>
{
private readonly string _connectionString;
public MainContextFactory(string connectionString)
{
_connectionString = connectionString;
}
public MainContext Create()
{
return new MainContext(_connectionString);
}
}
在Startup.cs中,我有:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
services.AddTransient<IDbContextFactory<MainContext>>(d =>
new MainContextFactory(Configuration["ConnectionStrings:Primary"]));
我从我的存储库项目(目标是.NET 4.7.2)中调用它,并且包含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;
}
调用基本存储库类:
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;
}
}
问题:
答案 0 :(得分:0)
要关联Oracle Provider:
使用MVC5中web.config中的值更新add.config:
<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>
然后在startup.cs中的services.AddMvc()之后添加:
services.AddScoped(provider =>
{
return new OracleDbContext(Configuration["ConnectionString"]);
});
贷记到Tony Sneed Post。