NET Standard vs Net Core App: when creating .NET C

2020-03-01 11:05发布

问题:

I am trying to develop my projects to be cross platform. I have created several class libraries in this way:

But, when I was using Entity Framework to scaffold my DB, the nuget packages needed didn't install except when used in Console application.

Here is the difference, the console app references the .NET Core:

And the Class library references NET Standard:

So why they are both under .NET Core but referencing different libraries? Are both of them Cross Platform or only the console which is using .NET Core? And should I avoid using Class libraries in this case?

回答1:

"So why they are both under .NET Core but referencing different libraries?"
The recommended NET version to create libraries for is now Net Standard Library. That is why the default when you create a class library is the net standard version instead of Net Core. You could manually change that but is not recommended see also this blog post about Introducing .NET Standard Library.

Are both of them Cross Platform or only the console which is using .NET Core?
Yes, they are both cross platform.

And should I avoid using Class libraries in this case?
No, by all means use Class Libraries every time is possible. This was not the original intent of Microsoft in the beginning but they change their mind later, so not all packages are yet compatible with .net standard that's is why you can not use a the net standard version to have a scaffold project because "Microsoft.EntityFrameworkCore.Tools" at the moment is only compatible with Net core, but it will probably be compatible with DotNet Standard soon

As I said if you really want to keep the scaffold option, then you can use a class library project and change manually in the project.json the .net version, then once is compatible you can switch back to .net standard. Your other projects such as a service or data layer depending how you want to keep the separation can be a class library with dotnet standard and they will be able to work correctly.

Or you could have a console application (.net core) appart from your original solution and copy and paste into your real class library using .net standard. (This is what we are currently doing in project)