c# - .NET Core 类库使用 EFCore DbContext 作为 Nuget 没有找到 EFCore
问题描述
我正在尝试将数据访问服务构建为 .NET Core 3.1 类库,然后使其成为可以在不同解决方案中重用的 nuget 包,而无需在每个解决方案中搭建数据库和构建存储库类。
类库代码经过测试并且工作正常,但随后将nuget安装在另一个解决方案中,调用数据服务中的方法时会引发此错误:
System.IO.FileNotFoundException:'无法加载文件或程序集'Microsoft.EntityFrameworkCore,版本 = 5.0.4.0,文化 = 中性,PublicKeyToken = adb9793829ddae60'。该系统找不到指定的文件
显然,使用 nuget 的解决方案或 nuget 本身缺少对 EntityFrameworkCore 的依赖,但我不确定是哪个以及为什么。
从我安装 nuget 的解决方案中查看解决方案资源管理器中的 DataService.dll 时,它没有显示该 .dll 的任何依赖关系,它与其他已安装的 nuget 一起使用。我已确保 nuget restore 由部署管道运行(使用 Azure DevOps 释放 nuget),据我所知,它正在获取所有 EFCore 依赖项。
Azure DevOps 在 .nuspec 中生成:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>SoRDataAccess</id>
<version>1.1.4</version>
<authors>VssAdministrator</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Description</description>
<dependencies />
</metadata>
</package>
有没有人成功地做到这一点或知道这应该是可能的。我开始担心这可能与 EFcore 无关,但我还没有找到任何可以证实的东西。
解决方案
您应该能够创建自己的 .nuspec,然后将其打包。
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>MyPackageId</id>
<version>$version$</version>
<title>My Package Title</title>
<authors>My Company Name</authors>
<owners>My Company Name</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A description of our package</description>
<copyright>Copyright 2021</copyright>
<dependencies>
<group targetFramework=".NETStandard2.0"> <!-- Libraries *should* target .NET Standard in most cases -->
<dependency id="SomeOtherNugetPackageInSameSolutionThatShouldHaveSameVersion" version="$version$" />
<dependency id="Microsoft.Azure.ServiceBus" version="4.1.1" /> <!-- Specify your external dependencies and their required version here -->
</group>
</dependencies>
</metadata>
<files>
<file src="bin\$configuration$\netstandard2.0\MyLibrary.dll" target="lib\netstandard2.0" />
</files>
</package>
在 Azure DevOps 中构建包时,您应该能够选中一个框以将 $version$ 替换为版本。否则,您可以对其进行硬编码。取决于您的团队如何进行版本控制。
重要的部分是<dependencies>
部分。
推荐阅读
- javascript - 数字列表到数字列表
- azure - Azure Pipelines YAML:如何摆脱“构建后:获取源”步骤?
- javascript - 构建或打包 nodejs 脚本,使其自包含
- r - 如何更改核密度级别图上的阴影颜色
- node.js - 无法为 nodejs web3 推送到 heroku - 它正在尝试找到 web3@1.0.0-beta.42 postinstall
- rust - 如果没有要返回的内容,如何从泛型函数返回有意义的内容?
- qt - 用 Qt 实现 3D 着色器到 2D 对象?
- java - JVisualVM 的堆转储中的“大小”是如何计算的
- sql - 如何加入不同的 CONCAT 表达式?
- sql - SQL 加法公式