问题
I can't figure out how should I set up dependencies (where to add EntityFramework nuget packages) in this scenario:
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
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
project, this is .NET Standard 2.0 DLL library. I have all application logic here. It depends onCore.Persistence
because it makes database queries andCore.Domain
because it produces bussiness objects from query results.Client.ConsoleClient
project. It makes .NET Framework 4.7.2 executable. It is supposed to depend only onCore.Application
, but I have a problem here.Client.WindowsClient
project which I don't want to focus in this question.
So, this is what I have done:
The problem is, that I'm getting System.IO.FileLoadException
when I try to call method from Core.Application
.
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.
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.
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.
How to fix this properly?
回答1:
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
A possible solution is to add the NuGet
dependency to the Full NET Framework
project, as you did.
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>
Note that I am using package references in the NET Standard
projects.
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 referencing Entity Framework
<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 referencing 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 referencing 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>
回答2:
The new SDK format csproj
files don't play terribly well with the legacy format project files.
But fear not as .NET Framework console apps can use the SDK format!
Make sure you have your work committed to source control, or make a copy of the folder, and then do the following:
Delete
Properties\AssemblyInfo.cs
fromClient.ConsoleClient
. We won't be needing this any more as the contents of that file now go into the project file.Delete
packages.config
- again, Nuget references will be stored in the project file - that's if you need any Nuget references after we referenceCore.Application
later.Open
Client.ConsoleClient.csproj
in a text editor and change the contents to:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net472</TargetFramework> </PropertyGroup> </Project>
Reload the project in Visual Studio.
Add a reference to
Core.Application
and add any Nuget packages you need.If you had any content in
Properties\AssemblyInfo.cs
other than versions at 1.0.0.0, right click the project in Solution Explorer and click Package. Add the details you need and then save:
That's it, although there are 2 others things you might need to do depending on your circumstances:
If there are files which should be excluded, you'll need to exclude them, as the new project format includes all relevant file types by default.
You might have to set the language version. In my Visual Studio 2019 Preview,
latest
(latest minor version of C#) is the default so I don't need to do this.
来源:https://stackoverflow.com/questions/57006359/how-to-set-dependencies-when-i-use-net-standard-2-0-dll-libraries-with-a-net-f