面向.NET 4.7.2的ASP.NET Core上的ODP.NET和EF6配置 [英] ODP.NET and EF6 configuration on ASP.NET Core targeting .NET 4.7.2

查看:102
本文介绍了面向.NET 4.7.2的ASP.NET Core上的ODP.NET和EF6配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得用于在项目中使用Oracle Provider(ODP.NET)的实体框架.

I can't get Entity Framework to use the Oracle Provider (ODP.NET) with my project.

设置:

  • 针对.NET Framework 4.7.2的ASP.NET Core MVC 2.1
  • EntityFramework 6.2
  • ODP.NET 18.3(Oracle.ManagedDataAccess和Oracle.ManagedDataAccess.EntityFramework)

尽管我更喜欢使用EF Core,但我不能这样做,因为Oracle尚不支持EF Core,仅支持.NET Core.

Although I'd prefer to use EF Core, I can't because Oracle isn't supporting EF Core yet, just .NET Core.

我收到的错误表明该应用程序正在尝试使用SQL Server驱动程序.

The errors I'm receiving indicate that the application is trying to use the SQL Server driver.

我无法在线找到适合我的方案的示例.带有EF6/ODP.NET的MVC5,或带有EF的Oracle的.NET Core示例.

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.

我的假设是,问题在于MVC5通过web.config/app.config对其进行配置.我假设我需要在start.cs中配置Oracle,但是需要正确的语法.

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.

我为Context类编写的代码:

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);
    }
}

在Startup.cs中,我有:

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"]));

我从我的存储库项目(目标是.NET 4.7.2)中调用它,并且包含MainContext:

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;
    }

调用基础存储库类:

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;
    }
}

问题:

  1. 我需要更新或添加什么才能使其称为ODP.NET提供程序?
  2. 是否有更好的配置方法?

推荐答案

要关联Oracle Provider:

To associate the Oracle Provider:

使用MVC5中web.config中的值更新add.config:

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>

然后在startup.cs中的services.AddMvc()之后添加:

Then add after the services.AddMvc() in startup.cs:

services.AddScoped(provider =>
            {
                return new OracleDbContext(Configuration["ConnectionString"]);
            });

贷记到 Tony Sneed Post .

这篇关于面向.NET 4.7.2的ASP.NET Core上的ODP.NET和EF6配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆