Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 209
Joe Jazdzewski Avatar asked Feb 07 '18 22:02

Joe Jazdzewski


People also ask

Can I use Entity Framework core with net standard?

NET Standard Library: the EF Core tools don't support the . NET Standard framework: they can only target .

Is .NET Core compatible with .NET standard?

NET Standard 1.3 will be compatible with apps that target . NET Framework 4.6, . NET Core 1.0, Universal Windows Platform 10.0, and any other platform that supports .


1 Answers

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]

like image 119
NightOwl888 Avatar answered Sep 21 '22 17:09

NightOwl888