我正在尝试使用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="User Id=UID;Password=****;Server=Server;Persist Security Info=True""
}
}
以下链接用作参考。
aspnet core 1.x with EF6(基于1.x,但有有用信息)
答案 0 :(得分:2)
经过多次尝试,我终于能够解决这个问题。
Devart.Data
,DevArt.Data.Oracle
。 Devart.Data.Oracle.Entoty.EF6
在同一个程序集中。DevArt.Data.Entity.OracleEntityProviderServicesConfiguration
public class OracleDbConfiguration : OracleEntityProviderServicesConfiguration// : DbConfiguration { public OracleDbConfiguration() { SetProviderServices("Devart.Data.Oracle", OracleEntityProviderServices.Instance); SetProviderFactory("Devart.Data.Oracle", OracleProviderFactory.Instance); } }
用它装饰你的DbContext类。 E.g。
[DbConfigurationType(typeof(OracleDbConfiguration))]
在DbContext类上创建一个静态构造函数并像这样填充
static MyEntities() { var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance; config.CodeFirstOptions.ColumnTypeCasingConventionCompatibility = false; }
当我有机会时,我会上传一个包含完整解决方案的简单应用程序。