当依赖于 COM 引用时如何使用`dotnet ef migrations add XYZ`?

How to use `dotnet ef migrations add XYZ` when there is a dependency on a COM reference?

我有一个带有 EF Core 的 C# 项目依赖于其他项目,其中一个依赖于 COM 引用。到目前为止一切正常(我认为 Visual Studio 的更新打破了这一点)。

一切都在 VisualStudio 中编译。但是在命令行上,dotnet build returns 这个错误:

C:\Program Files\dotnet\sdk.0.101\Microsoft.Common.CurrentVersion.targets(2929,5): error : MSB4803: The task "ResolveComReference" is not supported on the .NET Core version of MSBuild. Please use the .NET Framework version of MSBuild.

所以,简而言之,我不能再使用 EF Core Migrations,因为它依赖于 dotnet build。如果我尝试 运行 dotnet ef migrations add XYZ 我得到这个错误:

Build started...
Build failed. Use dotnet build to see the errors.

我这里的主要程序(带有迁移)是 .NET 5.0,使用 COM 引用的依赖项针对 .NetCore3.1(如果有帮助,我可以重新定位到 5.0)。

那么我如何告诉 EF Migrations 使用另一个 MSBuild,或者在这不是问题的情况下回滚到某些以前的工具版本,或者使我的 COM 引用不成问题,或者以其他方式解决这个问题?

提前致谢。

好吧,我找到了解决方法。不确定这是最好的方法,但它似乎有效。

来自this blog

  1. 引用 COM dll 并重建。 Visual Studio 将生成互操作 dll。它将被命名为 Interop.MyComDLL.dll。它位于构建输出目录中。
  2. 将生成的 Interop dll 复制到应用程序中的某个位置(我只是在根应用程序级别使用了 /dlls 文件夹)。
  3. 删除 COM dll 引用。
  4. 向 Interop dll 添加直接引用(依赖项 > 添加引用... > 在 Visual Studio 中浏览)

我可以补充一下:

  1. 用“copy local = yes”参数标记新的依赖关系。
  2. [编辑]还有“embed Interop Types = yes”参数。