安装 VS2019 后无法在 VS2015 中构建 .NET 4.5

Can't build .NET 4.5 in VS2015 after VS2019 install

我们在 Visual Studio 2015 年有了这个(相当大的)解决方案,它由 60 个 class 库项目和一些 MVC web 项目组成,所有项目的目标框架都设置为 .NET 4.5(不是 4.5.1 之类的,普通的 4.5)。 所以我们计划很快切换到 Visual Studio 2019,所以我和其他一些人安装了它以掌握它。原来是安装坏了"something",所以我们这个庞大的工程已经无法编译了

目前我所知道的是,在构建解决方案主 MVC 项目期间,编译器会为 8 个依赖项目抛出以下警告代码 "MSB3275"。它们唯一的共同点似乎是它们都引用 Entity Framework 6.2.0。警告后,主 MVC 项目无法编译,说明找不到所有引用的 dll。这是警告(为了保护隐私,项目名称已被隐藏):

C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\REDACTED.DataAccess\bin\Release\REDACTED.DataAccess.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\Utility.TextGetterLib\bin\Release\Utility.TextGetterLib.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\Utility.ExportToFileLib\bin\Release\Utility.ExportToFileLib.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\REDACTED.DataManager\bin\Release\REDACTED.DataManager.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\Utility.KendoView\bin\Release\Utility.KendoView.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\Utility.EmailServerLib\bin\Release\Utility.EmailServerLib.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\ExcelExporter\bin\Release\ExcelExporter.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
C:\Program Files (x86)\MSBuild.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3275: The primary reference "D:\Projects\Git\REDACTED\Utility.UserHelperLib\bin\Release\Utility.UserHelperLib.dll" could not be resolved because it has an indirect dependency on the assembly "Microsoft.SqlServer.Types, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" which was built against the ".NETFramework,Version=v4.6" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".

所以就像它在错误中所说的那样,间接依赖现在不知何故是 .NET 4.6 而不是 4.5。如果我将项目的目标框架设置为 4.6,它编译得很好。

我已经尝试了两天来解决这个问题,但我已经束手无策了,我几乎尝试了每个项目依赖项的所有方法,但似乎没有任何效果。我不知道我还应该提供什么信息来帮助解决这个问题,但是如果有人想尝试破解这个问题,我会尝试提供必要的信息。

编辑 - 替代解决方案

因此,在使用此线程中的解决方案 posted 之后,我想出了一个替代修复方法(更持久一些),我将在此处 post 以防其他人偶然发现此页面。 所讨论的项目是从 .NET 3.5 时代幸存下来并经过多年升级的项目。这意味着仍然有一些以前的开发人员编写的遗留代码没有被正确删除或重构。显然 Microsoft.SqlServer.Types 依赖项在我们的代码库中几年前就被弃用了,那么为什么它突然成为一个问题呢? 原来这是在主要网络项目 web.config 文件的最底部:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-15.0.0.0" newVersion="15.0.0.0" />
  </dependentAssembly>

如您所见,它引用了最高版本 15,该版本在安装 VS2019 之前不存在。因此问题。这提出了两个解决方案:

1: 将最高版本设置为 14 使项目能够编译。

2: 完全删除该条目,因为它是一个已弃用的引用。

到此为止,希望这个帖子能帮助其他人解决 MSB3275 警告编译器代码。

您似乎从 GAC 引用了安装 VS2019 时更新的程序集。在VS2015中,打开解决方案并安装Microsoft.SqlServer.Types nuget package.