NET Standard与Net Core App:创建.NET Core Project时(使用控制台或类库)

时间:2022-03-09 23:09:51

I am trying to develop my projects to be cross platform. I have created several class libraries in this way:NET Standard与Net Core App:创建.NET Core Project时(使用控制台或类库)

我正在尝试将我的项目开发为跨平台。我用这种方式创建了几个类库:

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

但是,当我使用Entity Framework来构建我的数据库时,除了在Console应用程序中使用时,所需的nuget包没有安装。

Here is the difference, the console app references the .NET Core: NET Standard与Net Core App:创建.NET Core Project时(使用控制台或类库)

这是区别,控制台应用程序引用.NET Core:

And the Class library references NET Standard:

类库引用NET标准:

NET Standard与Net Core App:创建.NET Core Project时(使用控制台或类库)

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?

那么为什么他们都在.NET Core下但引用了不同的库?它们都是跨平台还是仅使用.NET Core的控制台?在这种情况下,我应该避免使用类库吗?

1 个解决方案

#1


5  

"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.

“那么为什么他们都在.NET Core下,但引用了不同的库?”用于创建库的推荐NET版本现在是Net Standard Library。这就是为什么创建类库时的默认值是净标准版本而不是Net Core。您可以手动更改但不建议另请参阅此博客文章介绍.NET标准库。

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

它们都是跨平台还是仅使用.NET Core的控制台?是的,它们都是跨平台的。

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

在这种情况下,我应该避免使用类库吗?不,无论何时都可以使用类库。这不是微软最初的意图,但他们后来改变了主意,所以并非所有软件包都与.net标准兼容,这就是为什么你不能使用net标准版本来拥有一个脚手架项目,因为“Microsoft。 EntityFrameworkCore.Tools“目前只与Net core兼容,但它可能很快就会与DotNet Standard兼容

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.

正如我所说,如果你真的想保留脚手架选项,那么你可以使用类库项目并在project.json .net版本中手动更改,然后一旦兼容你可以切换回.net标准。您的其他项目(如服务或数据层)取决于您希望如何保持分离,可以是具有dotnet标准的类库,它们将能够正常工作。

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)

或者您可以从原始解决方案中获得一个控制台应用程序(.net core)appart,并使用.net标准将其复制并粘贴到您的真实类库中。 (这是我们目前在项目中所做的事情)

#1


5  

"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.

“那么为什么他们都在.NET Core下,但引用了不同的库?”用于创建库的推荐NET版本现在是Net Standard Library。这就是为什么创建类库时的默认值是净标准版本而不是Net Core。您可以手动更改但不建议另请参阅此博客文章介绍.NET标准库。

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

它们都是跨平台还是仅使用.NET Core的控制台?是的,它们都是跨平台的。

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

在这种情况下,我应该避免使用类库吗?不,无论何时都可以使用类库。这不是微软最初的意图,但他们后来改变了主意,所以并非所有软件包都与.net标准兼容,这就是为什么你不能使用net标准版本来拥有一个脚手架项目,因为“Microsoft。 EntityFrameworkCore.Tools“目前只与Net core兼容,但它可能很快就会与DotNet Standard兼容

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.

正如我所说,如果你真的想保留脚手架选项,那么你可以使用类库项目并在project.json .net版本中手动更改,然后一旦兼容你可以切换回.net标准。您的其他项目(如服务或数据层)取决于您希望如何保持分离,可以是具有dotnet标准的类库,它们将能够正常工作。

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)

或者您可以从原始解决方案中获得一个控制台应用程序(.net core)appart,并使用.net标准将其复制并粘贴到您的真实类库中。 (这是我们目前在项目中所做的事情)