C# EF Core 2.1.4 FileLoadException System.ComponentModel.Annotations 4.2.0.0

C# EF Core 2.1.4 FileLoadException System.ComponentModel.Annotations 4.2.0.0,c#,.net,entity-framework,ef-core-2.1,C#,.net,Entity Framework,Ef Core 2.1,在将数据访问层从EF6升级到EF Core 2.1(.4)之后,我们遇到了一个带有System.ComponentModel.Annotations,Version=4.2.0.0的FileLoadException问题,这很奇怪,因为EF Core 2.1.4使用4.5.0.0,我们的解决方案中没有其他(我们可以找到)使用4.2.0.0 例如,我们的解决方案中有以下项目结构: DataAccess:(所有使用Microsoft.EntityFrameworkCore.SqlServer 2.1

在将数据访问层从EF6升级到EF Core 2.1(.4)之后,我们遇到了一个带有
System.ComponentModel.Annotations,Version=4.2.0.0
FileLoadException
问题,这很奇怪,因为EF Core 2.1.4使用
4.5.0.0
,我们的解决方案中没有其他(我们可以找到)使用
4.2.0.0

例如,我们的解决方案中有以下项目结构:

DataAccess:(所有使用Microsoft.EntityFrameworkCore.SqlServer 2.1.4版的项目)
-模型(仅使用Scaffold DbContext创建的模型)
-核心(抽象提供程序、持久化程序等)
-常见(DTO、具体提供者、持久化者等)

主要解决方案:(没有安装EF核心包的项目)
-一些项目(A)的项目参考模型、核心、通用

例如,即使项目A是一个简单的控制台应用程序,它只是从数据库加载某些内容并将其显示到控制台,但当在DataAccess.Core中的抽象提供程序级别对某些内容进行评估时,我们会得到以下异常:

System.IO.FileLoadException发生HResult=0x8013100 消息=无法加载文件或程序集“System.ComponentModel.Annotations,Version=4.2.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。定位的程序集清单定义与程序集引用不匹配。(来自HRESULT的异常:0x8013100)

奇怪的是,我们在任何地方都找不到
System.ComponentModel.Annotations,Version=4.2.0.0
,该版本甚至在Nuget中被跳过。
尝试此问题的公认答案:,通过将以下内容添加到我的
.csproj
文件中,似乎可以解决某些项目的问题,但不是所有项目的问题:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

它不是
4.2.0.0
,但我们不确定新版本号在哪里确定。这可能是我们遗漏或配置不正确的一些小问题,但如果能提供一些帮助来发现问题或指出应该查找的位置,我们将不胜感激。我们不必在每个
.csproj
文件中添加
AutoGenerateBindingRedirects
的解决方案将是@JRB注释中的理想内容,格式为可读性的答案:


这是一个微软似乎很难解决的现有问题,可能是因为它涉及多个开发小组之间的合作。它只发生在一些更复杂的解决方案中。解决方案非常简单。将绑定重定向更改为


而且你的问题消失的可能性非常合理。此bindingredirect可以自动应用于多个项目。测试导致您出现问题的原因。

我也有同样的问题。我已经通过实现以下帮助函数解决了这个问题:在开始时重定向程序集(在中建议):


这是一个微软似乎很难解决的现有问题。可能是因为它涉及多个开发组之间的合作。它只发生在一些更复杂的解决方案中。解决方案非常简单。将上面提到的bindingRedirect改为:你的问题消失的可能性非常合理。此bindingredirect可以自动应用于多个项目。测试导致您出现问题的原因。@JRB成功了,谢谢!您知道为什么它不能与
newVersion=“4.2.1.0”
一起使用吗?如何在多个项目中自动应用bindingredirect?由于许多不同的解决方案依赖于这些项目,我是否可以在基本DataAccess项目中更改/设置一些内容,以便在所有调用应用程序中强制执行绑定重定向?@JRB,如果你想把你的评论变成一个答案,我很乐意接受它jchristen:在它的当前形式中,“System.ComponentModel.Annotations”自动生成绑定重定向的错误是在EF Core 2.1中引入的,但之前也有类似的问题。在我的情况下,我总是需要在解决方案中手动更改(在app.config中)自动生成的绑定重定向,因为我在同一个解决方案中同时拥有.Net Framework(使用EF.Core的WCF服务)和.Net Framework项目(Windows服务)。在我上面的评论中,我应该说“在同一个解决方案中的多个项目中”。我会在不久的将来把我的评论修改成一个答案。@JRB你应该完全把这个作为答案,因为这对我来说很有用。非常感谢你!
<dependentAssembly>
  <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>
public static class FunctionsAssemblyResolver
{
    #region Public Methods

    public static void RedirectAssembly()
    {
        AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;
    }

    #endregion Public Methods

    #region Private Methods

    private static Assembly ResolveAssemblyOnCurrentDomain(object sender, ResolveEventArgs args)
    {
        var requestedAssembly = new AssemblyName(args.Name);
        var assembly = default(Assembly);

        AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssemblyOnCurrentDomain;

        try
        {
            assembly = Assembly.Load(requestedAssembly.Name);
        }
        catch
        { }

        AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblyOnCurrentDomain;

        return assembly;
    }

    #endregion Private Methods
}