带有EF6的aspnetcore 2.0和用于oracle的

时间:2017-08-16 16:01:52

标签: c# oracle asp.net-core entity-framework-6 dotconnect

我正在尝试使用dotConnect for Oracle(v 9.4)来使用Oracle。由于aspnet核心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."

这是我的CodeConfig班级

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类

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

    }
...
}
  

执行base(nameOrConnectionString)时会抛出异常。

启动时的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 个答案:

答案 0 :(得分:2)

经过多次尝试,我终于能够解决这个问题。

  1. QUOT;连接字符串应替换为'
  2. 而不是针对netcoreapp2.0,将您的aspnetcore 2.0应用程序定位到net461或任何高于此值的完整框架(在您的aspnetcore 2.0 csproj文件中查找net461)
  3. 将nu&gt; = 6.1从nuget添加到您的模态/实体程序集。参考Devart.DataDevArt.Data.OracleDevart.Data.Oracle.Entoty.EF6在同一个程序集中。
  4. 创建一个源自DevArt.Data.Entity.OracleEntityProviderServicesConfiguration
  5. 的OracleDbConfiguration类
    public class OracleDbConfiguration : OracleEntityProviderServicesConfiguration// : DbConfiguration
    {
        public OracleDbConfiguration()
        {
             SetProviderServices("Devart.Data.Oracle", OracleEntityProviderServices.Instance);
             SetProviderFactory("Devart.Data.Oracle", OracleProviderFactory.Instance);
        }
    }
    
    1. 用它装饰你的DbContext类。 E.g。

      [DbConfigurationType(typeof(OracleDbConfiguration))]

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

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

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