我正在尝试使用dotConnect for Oracle(v9.4)连接Oracle数据库。由于aspnet core 2.0已经完全支持引用旧的.NET框架,因此我正在尝试使用常规的.NET程序集(即包含我的DbContext的程序集以.NET 4.6.1为目标,并在我的asp.net core 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 中的 EF6(基于 1.x,但包含有用信息)
'
而不是"
,并将我的 .Net Core MVC 应用程序定位到net461
,似乎已经解决了问题。谢谢!!! - neilsimp1