NET Standard 与 Net Core App:创建 .NET Core 项目时(使用控制台或类库) [英] NET Standard vs Net Core App: when creating .NET Core Project (using console or class library)

查看:23
本文介绍了NET Standard 与 Net Core App:创建 .NET Core 项目时(使用控制台或类库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,当我使用实体框架来构建我的数据库时,所需的 nuget 包没有安装,除非在控制台应用程序中使用.

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

区别在于,控制台应用程序引用了 .NET Core:

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

并且类库引用了 NET Standard:

And the Class library references NET Standard:

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

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 版本现在是 Net Standard Library.这就是为什么创建类库时的默认版本是 net 标准版本而不是 Net Core.您可以手动更改它,但不建议您参阅此博客文章关于 介绍 .NET 标准库.

"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 的控制台?
是的,它们都是跨平台的.

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

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

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

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

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.

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

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 Standard 与 Net Core App:创建 .NET Core 项目时(使用控制台或类库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆