问题描述
更新到新软件包Microsoft.EntityFrameworkCore.SqlServer 1.1.2之后,尝试创建DBContext时出错:
After update to the new package Microsoft.EntityFrameworkCore.SqlServer 1.1.2 I got error when try to create DBContext:
我的基本DbContext
My base DbContext
public class SqlServerDbContext : DbContext
{
private readonly DatabaseOptions _databaseOptions;
protected SqlServerDbContext(DatabaseOptions databaseOptions)
{
if (string.IsNullOrEmpty(databaseOptions.ConnectionString))
throw new Exception("Database connection string is missed.");
_databaseOptions = databaseOptions;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_databaseOptions.ConnectionString);
}
}
我使用的数据库选项
public class DatabaseOptions
{
public string ConnectionString { get; set; }
}
创建上下文实例的地方
var dbOptions = new DatabaseOptions { ConnectionString = _connectionString };
DbContext = (TContext) Activator.CreateInstance(typeof(TContext), dbOptions);
// where TContext is derived class from SqlServerDbContext
我的所有软件包都已更新。 Visual Studio 2017 15.2(26430.6)。升级到1.1.2之前,一切正常。请帮助解决问题。
All my packages are updated. Visual Studio 2017 15.2 (26430.6). Before upgrade to 1.1.2 everything works fine. Please help to solve the problem.
推荐答案
由于您是在.net框架库中使用项目,因此自动生成的绑定重定向会出现问题(可能在即将发布的15.3更新/2.0 .net核心CLI中得到解决)。要解决此问题,请将其添加到您的 cpsroj
文件中(最好在< Import>
元素之前添加为 .targets
文件(如果有)):
Since you're using the project in a .net framework library, there's an issue with auto-generated binding redirects (might be resolved in the upcoming 15.3 update / 2.0 .net core CLI). To work around it, add this in your cpsroj
file (preferably before any <Import>
element for a .targets
file if present):
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
这应强制MSBuild创建/更新 YourProject.dll.config
文件,其中包含必要的绑定重定向。
This should force MSBuild to create / update a YourProject.dll.config
file containing the necessary binding redirects.
这篇关于无法加载文件或程序集Microsoft.Extensions.DependencyInjection.Abstractions,版本= 1.1.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!