尝试在 .Net Standard 项目中设置实体框架核心 [英] Trying to set-up Entity Framework core in .Net Standard project

查看:31
本文介绍了尝试在 .Net Standard 项目中设置实体框架核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在 .NET Standard 2.0 项目中轻松设置我的 EntityFrameworkCore.我正在关注这个教程,但它需要 .NET Core 或 Framework.

I was wondering if I could set-up my EntityFrameworkCore in a .NET Standard 2.0 project easily. I was following this Tutorial but it requires either .NET Core or Framework.

当我走到这一步时:

Scaffold-DbContext "Server=(localdb)mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

我收到此错误:

启动项目projectName"以框架.NETStandard"为目标.没有与此框架关联的运行时,针对它的项目无法直接执行.要将 Entity Framework Core Package Manager Console Tools 与此项目一起使用,请添加一个面向 .NET Framework 或 .NET Core 的可执行项目并引用此项目,并将其设置为启动项目;或者,更新此项目以跨目标 .NET Framework 或 .NET Core.

Startup project 'projectName' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core.

我想知道我是否可以在 .NET Standard 中设置我的实体,或者是否有我应该做的最佳实践?

I am wondering if I could set-up my entity in .NET Standard or if there a best practice that I should be doing instead?

推荐答案

启动项目projectName"以框架.NETStandard"为目标.没有与此框架关联的运行时,针对它的项目无法直接执行.要将 Entity Framework Core Package Manager Console Tools 与此项目一起使用,请添加一个面向 .NET Framework 或 .NET Core 的可执行项目并引用此项目,并将其设置为启动项目;或者,更新此项目以跨目标 .NET Framework 或 .NET Core.

Startup project 'projectName' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core.

错误消息的意思是:不存在可执行 .NET Standard 项目.它没有运行时,因为它只是一种用于多个不同运行时的类型转发机制.

The error message means this: There is no such thing as an executable .NET Standard project. There is no runtime for it because it is simply a type-forwarding mechanism for multiple different runtimes.

在编程术语中,它有点像尝试实例化接口.您无法执行此操作,因为没有要运行的实现.

In programming terms, it is a bit like trying to instantiate an interface. You can't do it because there is no implementation to run.

解决方案是为您的应用程序选择一个可执行平台.您可以从可执行文件中引用任意数量的 .NET Standard 库(只要它们与您的运行时兼容),但可执行文件本身不能在 .NET Standard 上运行.它必须以 .NET Framework 或 .NET Core 等平台为目标才能执行.

The solution is to pick an executable platform for your application to run on. You can reference as many .NET Standard libraries as you like from your executable (as long as they are compatible with your runtime), but the executable itself cannot be run on .NET Standard. It must target a platform such as .NET Framework or .NET Core in order to execute.

换句话说,为了使用命令来构建数据库,您必须以运行时为目标.因此,您要么需要在定位主要可执行项目时运行此命令,要么将新的可执行项目添加到解决方案中以运行它.

In other words, in order to use a command to scaffold your database, you have to target a runtime. So you will either need to run this command while targeting your main executable project or add a new executable project to your solution to run it on.

您可以使用选项在 CLI 上运行您的命令来执行此操作--startup-project=[Path_to_your_main_Project]

You can do this running your command on the CLI with the option --startup-project=[Path_to_your_main_Project]

这篇关于尝试在 .Net Standard 项目中设置实体框架核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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