带有 EF6 和 dotConnect for oracle 的 aspnetcore 2.0

aspnetcore 2.0 with EF6 and dotConnect for oracle

我正在尝试通过 dotConnect for Oracle(v 9.4) 使用 Oracle。由于 aspnet core 2.0 完全支持引用旧的 .net 框架,我正在尝试使用常规的 .net 程序集连接到数据库(即包含我的 DbContext 的程序集以 .net 4.6.1 为目标,并在我的 asp.net 核心 2 项目).

我面临以下问题

"System.TypeInitializationException: 'The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.'

Inner Exception: FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified."

这是我的CodeConfigclass

public class CodeConfig : DbConfiguration
{
        public CodeConfig()
        {
            SetProviderServices("Devart.Data.Oracle", Devart.Data.Oracle.Entity.OracleEntityProviderServices.Instance);
            SetProviderFactory("Devart.Data.Oracle", new Devart.Data.Oracle.OracleProviderFactory());
        }
}

我的 DbContext class

[DbConfigurationType(typeof(CodeConfig))]
public partial class MyEntities : DataContext, IMyStoredProcedures
{
  public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString)
        {
            Configure();
        }

        private void Configure()
        {
            this.Configuration.AutoDetectChangesEnabled = true;
            this.Configuration.LazyLoadingEnabled = true;
            this.Configuration.ProxyCreationEnabled = true;
            this.Configuration.ValidateOnSaveEnabled = true;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }    

    }
...
}

Exception is thrown when base(nameOrConnectionString) is executed.

启动中的 DI

var dbContext = new MyEntities(Configuration["Data:DefaultConnection:ConnectionString"]);
services.AddScoped<IDataContextAsync>(provider => dbContext);
services.AddScoped<IMyStoredProcedures>(provider => dbContext);

Appsettings.json

"Data": {
    "DefaultConnection": {
      "ConnectionString": "metadata=res://Org.MyApp.Entities/MyModel.csdl|res://Org.MyApp.Entities/MyModel.ssdl|res://Org.MyApp.Entities/MyModel.msl;provider=Devart.Data.Oracle;provider connection string=&quot;User Id=UID;Password=****;Server=Server;Persist Security Info=True&quot;"
    }
  }

以下链接仅供参考。

  1. Getting started Asp.net core with EF6

  2. aspnet core 1.x with EF6(基于1.x但有有用的信息)

经过多次尝试,我终于破解了这个。

  1. ”在连接字符串中应替换为 '
  2. 不要以 netcoreapp2.0 为目标,而是将您的 aspnetcore 2.0 应用程序以 net461 或任何更高的完整框架为目标(在您的 aspnetcore 2.0 csproj 文件中查找 net461)
  3. 将 EF >=6.1 从 nuget 添加到您的 modal/entities 程序集。参考 Devart.DataDevArt.Data.OracleDevart.Data.Oracle.Entoty.EF6 在同一个程序集中。
  4. 创建一个 class OracleDbConfiguration 派生自 DevArt.Data.Entity.OracleEntityProviderServicesConfiguration
public class OracleDbConfiguration : OracleEntityProviderServicesConfiguration// : DbConfiguration
{
    public OracleDbConfiguration()
    {
         SetProviderServices("Devart.Data.Oracle", OracleEntityProviderServices.Instance);
         SetProviderFactory("Devart.Data.Oracle", OracleProviderFactory.Instance);
    }
}
  1. 用它装饰你的 DbContext class。例如

    [DbConfigurationType(typeof(OracleDbConfiguration))]

  2. 在您的 DbContext class 上创建一个静态构造函数并像这样填充它

static MyEntities()
{
    var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
    config.CodeFirstOptions.ColumnTypeCasingConventionCompatibility = false;
}

有机会的话,我会上传一个包含完整解决方案的简单应用程序。