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?