当我将.NET Standard 2.0 DLL库与.NET Framework控制台应用程序一起使用时,如何设置依赖项? [英] How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?
问题描述
在这种情况下,我不知道应该如何设置依赖项(在何处添加EntityFramework nuget包):
I can't figure out how should I set up dependencies (where to add EntityFramework nuget packages) in this scenario:
-
Core.Persistence
项目,该项目可编译为.NET Standard 2.0 DLL库。我有Entity Framework 6,用于EF,DbContext等的数据库实体类。它应该仅依赖于EntityFrameworkCore
。
Core.Persistence
project which compiles to .NET Standard 2.0 DLL library. I have Entity Framework 6, database entity classes for EF, DbContext etc. It is supposed to depend just onEntityFrameworkCore
.
Core.Domain
项目,该项目也可以编译为.NET Standard 2.0 DLL库。我想将我的业务对象POCO类放在这里。
Core.Domain
project which also compiles to .NET Standard 2.0 DLL library. I want to put my business object POCO classes here. This is supposed to have no dependencies.
Core.Application
项目,这是.NET Standard 2.0。 DLL库。我这里有所有应用程序逻辑。它取决于 Core.Persistence
,因为它可以进行数据库查询,而取决于 Core.Domain
,因为它可以从查询结果中生成业务对象。
Core.Application
project, this is .NET Standard 2.0 DLL library. I have all application logic here. It depends on Core.Persistence
because it makes database queries and Core.Domain
because it produces bussiness objects from query results.
Client.ConsoleClient
项目。它使.NET Framework 4.7.2可执行。它应该仅依赖于 Core.Application
,但我在这里遇到问题。
Client.ConsoleClient
project. It makes .NET Framework 4.7.2 executable. It is supposed to depend only on Core.Application
, but I have a problem here.
Client.WindowsClient
项目,我不想关注这个问题。
Client.WindowsClient
project which I don't want to focus in this question.
所以,这就是我所做的:
So, this is what I have done:
问题是,当我尝试从 Core调用方法时,出现了
。 System.IO.FileLoadException
。应用程序
The problem is, that I'm getting System.IO.FileLoadException
when I try to call method from Core.Application
.
它表示找不到 System.Interactive.Async
文件(这是 EntityFrameworkCore
的依赖项。在我将此文件添加为依赖项之后-还有其他 System.IO.FileLoadException
错误。
It says that it cannot find System.Interactive.Async
file (which is dependency of EntityFrameworkCore
). After I add this file as dependency - there are other System.IO.FileLoadException
errors.
所以,暂时已将EF6 Core NuGet程序包添加到我的 Client.ConsoleClient
中,并且 System.IO.FileLoadException
的问题消失了,但是我觉得我做错了。
So, temporarily I have added EF6 Core NuGet package to my Client.ConsoleClient
, and problems with System.IO.FileLoadException
are gone, but I feel I'm doing something wrong.
此刻,我发现Visual Studio不会从 Core.xxx <复制DLL文件。 / code>项目输出到
Client.ConsoleClient
项目输出,这就是为什么我出错的原因。
At this moment I figured out, that Visual Studio is not copying DLL files from Core.xxx
projects outputs into Client.ConsoleClient
project output, and that's why I'm getting errors.
如何正确解决此问题?
推荐答案
这是众所周知的在GitHub上登录的旧伤害位于:
依赖项不会通过项目引用
This is a wellknown and quite old hurt logged on GitHub at:
dependencies don't flow from new NET Standard
project to old desktop projects through project references link
可能的解决方案是添加 NuGet
对 Full NET Framework
项目,就像您所做的那样。
A possible solution is to add the NuGet
dependency to the Full NET Framework
project, as you did.
另一个建议是将以下内容包括在<$ c中 Full NET Framework项目
的$ c> .csproj 项目文件也对我有用。
The other suggestion to include the following to the .csproj
project file of the Full NET Framework project
is also working for me.
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
请注意,我在 NET Standard $ c中使用包引用$ c>项目。
到目前为止,看来 NET Standard
项目是最好的选择作为 NuGet
包使用,因为这些将把任何依赖引用作为 NuGet
包包含到目标项目中。
As for now, it looks like NET Standard
projects are best to be consumed as NuGet
packages, as these would include any dependent references as NuGet
packages into the target project.
Core.Persistence.csproj 引用实体框架
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
</ItemGroup>
</Project>
Core.Application.csproj 引用 Core.Persistence
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core.Persistence\Core.Persistence.csproj" />
</ItemGroup>
</Project>
ConsoleClient.csproj 引用 Core.Application
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<!-- ... -->
</PropertyGroup>
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<!-- ... --->
<ItemGroup>
<ProjectReference Include="..\Core.Application\Core.Application.csproj">
<Project>{067b3201-3f65-4654-a0fb-e8fae521bf29}</Project>
<Name>Core.Application</Name>
</ProjectReference>
</ItemGroup>
</Project>
这篇关于当我将.NET Standard 2.0 DLL库与.NET Framework控制台应用程序一起使用时,如何设置依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!