无法加载文件或程序集Microsoft.Extensions.DependencyInjection.Abstractions,版本= 1.1.0.0 [英] Could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0

查看:1042
本文介绍了无法加载文件或程序集Microsoft.Extensions.DependencyInjection.Abstractions,版本= 1.1.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新到新软件包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:


System。发生IO.FileLoadException HResult = 0x80131040

消息=无法加载文件或程序集
'Microsoft.Extensions.DependencyInjection.Abstractions,
Version = 1.1.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60'或
其依赖项之一。找到的程序集的清单定义
与程序集引用不匹配。 (来自HRESULT的异常:
0x80131040)源= Microsoft.EntityFrameworkCore StackTrace:位于
处Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions
选项)位于
Services.Infrastructure.Data。
中的SqlServerDbContext..ctor(DatabaseOptions
databaseOptions)C:\src\后端\Packages\Services.Infrastructure\Data\SqlServerDbContext.cs:line
16 at $
中的b $ b Translations.Api.Data.TranslationsDbContext..ctor(DatabaseOptions
databaseOptions)C:\src\后端\模块\Translations\Translations.ApiAData\ TranslationsDbContext.cs:line
16

System.IO.FileLoadException occurred HResult=0x80131040
Message=Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options) at Services.Infrastructure.Data.SqlServerDbContext..ctor(DatabaseOptions databaseOptions) in C:\src\backend\Packages\Services.Infrastructure\Data\SqlServerDbContext.cs:line 16 at Translations.Api.Data.TranslationsDbContext..ctor(DatabaseOptions databaseOptions) in C:\src\backend\Modules\Translations\Translations.Api\Data\TranslationsDbContext.cs:line 16

我的基本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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆