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

8

52

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?

Discography answered 7/2, 2018 at 22:0 Comment(2)
Check this out. Also make sure in your .csproj you have <TargetFramework>netstandard2.0</TargetFramework> and you are using Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.0.0Denitrify
I am still getting the same error and I have Microsoft.EntityFrameworkCore.SqlServer 2.0.1 installed if that makes a differenceDiscography
S
46

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]

Sporran answered 7/2, 2018 at 22:33 Comment(7)
I added a .NET CORE console application to the solution was able to target .NET Standard project to inherit the modelsDiscography
I get the gist of this, but it's still a little counter intuitive to me. If I run ef commands against a .NETStandard project, it should be able to see that and handle the situation. The executable framework can be either as it should gen the same code, right? Anyway, to add to the answer, you can fix by using the Project and StartupProject switches: add-migration "some migration" -Project Some.NetStandard.Data -StartupProject SomeNetFrameworkOrNetCore.ProjectAleece
So in order to use scaffolding, i have to create some executable type project, then run the scaffold to create all classes...and either manually copy to my .net standard class library or do some tricks to make these files accessible in my .netstandard class library. This is a hack. There must be a bigger better approach than this. I am not upset at this SO post, more the product.Quandary
@Quandary I'm currently in the same situation as you are. My solution would be to create a DummyScaffoldingProject targeting .NET Core and then moving all the generated classes over to a .NET Standard project. Have you encountered a better solution? I agree with Daniel that it is counter intuitive. I guess could write a PowerShell script that abuses the DummyScaffoldingProject and automatically moves the generated files to my .NET Standard project? This would sort of replace a dialog within VS with a script from outside. Any thoughts on this?Slobber
There are basically 2 ways to go here. If this is an application layer that is not ever going to be run outside of .NET Core, then the best solution is to target .NET Core instead of .NET Standard. If you need the library to target .NET Standard for some reason, you can have a .NET Core targeted scaffolding project (that uses the same default namespace) and then use the linked folder feature to compile the classes that are generated with .NET Core into your .NET Standard project.Sporran
@Sporran Would you have time to suggest/comment a possible solution to my similar SO post here? My executable project (Startup project) is .NET 4.8, and the .NET Standard library project is 2.0. I have been able to scaffold everything on .NET Standard project but when I run PM> Update-Database -Project MyStandardLibraryProj it creates the database on my user directory (instead of installation directory bin\Debug). Thank you in advance.Keratin
There's got to be a way to share code cause my EF framework needs to be able to be shared so i don't have to duplicate codeVocation
G
14
  1. Right-clicking the .NET Core app in your project

  2. Clicking Set as StartUp Project

enter image description here

Gastrostomy answered 28/3, 2019 at 15:38 Comment(1)
Did not work for me. Still getting the same error. My project is .NETFramework 4.8Keratin
H
11

You can run dotnet ef migrations against .NET Standard projects by creating a .NET Core project, adding your .NET Standard project as a reference, then using the --project and --startup-project to specify which project to run the migrations against.

dotnet ef migrations add MyNewMigrationName --project [pathToNetStandardProject] --startup-project [pathToNetCoreProject]

Huertas answered 2/11, 2020 at 15:26 Comment(1)
It works for me. I ran the command from my solution directory.Illogic
T
3

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

Transmute answered 3/5, 2018 at 23:20 Comment(0)
F
3

The issue here is this particular line in your .csproj file

<TargetFramework>netstandard2.0</TargetFramework>

change it to

<TargetFramework>netcoreapp2.1</TargetFramework>

or whatever the version of .net core your main project is targetting, It worked for me, it should work for you too!! :)

Freckle answered 11/8, 2020 at 3:7 Comment(2)
Changing the TargetFramework worked for me. Had to change referenced project's TargetFramework as well.Xerophagy
This defeats the purpose that your database maybe used in .Net Framework 4.6.2 and in a project that is .Net5. I have this exact issue since PhotonServer only supports .Net Framework 4.xInanity
M
2

I will share my experience as well, maybe it saves time for others.

In my case I had set multiple projects as startup projects, running the migration gave me this error which the questions state.

SOLUTION:

I changed multiple startup projects to a single project, and migration generated successfully!

Mcbrayer answered 10/2, 2021 at 19:40 Comment(0)
G
1

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://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#other-target-frameworks

Gyre answered 23/7, 2019 at 20:40 Comment(0)
I
1

Step1:
Add ProjectReference your project .netstandar".

Step2:
You can use the parameter -Project "your project .netstandar".

Inion answered 12/10, 2020 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.