Trying to set-up Entity Framework core in .Net Standard project

一曲冷凌霜 提交于 2019-11-30 06:40:50

问题


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.

When I got to this step:

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

I got this error:

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.

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?


回答1:


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.

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.

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.

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




回答2:


First answer here. I'm actually trying to do this also. I got the Scaffolding but Net Standard doesn't seem to load the Extensions for table properties.

Make sure you install the EntityFrameworkCore.Tools package for EF. You only need 4.6.1 support which is the default. If I figure out how to fix the extensions issue I'll post here.

These two are required. Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.Tools




回答3:


  1. Right-clicking the .NET Core app in your project

  2. Clicking Set as StartUp Project




回答4:


The following works from the dotnet CLI assuming you already have a executable startup project in the solution:

dotnet ef dbcontext scaffold "Server=myServerName; Database=dbName; User Id=someUser; Password=myPassword123;" Microsoft.EntityFrameworkCore.SqlServer  --project ../MyLibraryProject/ -c MyDbContext -o FolderName

you can find more information here:

https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#other-target-frameworks



来源:https://stackoverflow.com/questions/48673987/trying-to-set-up-entity-framework-core-in-net-standard-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!