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

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

问题描述

我想知道是否可以轻松地在.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软件包管理器控制台工具与此项目一起使用,请添加一个针对.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软件包管理器控制台工具与此项目一起使用,请添加一个针对.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标准项目。它没有运行时,因为它只是用于多个不同运行时的类型转发机制。

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项目中设置Entity Framework核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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