1

I am trying to add entity framework to a .Net Standard 2.0 Class Library.

I have tried creating the classes from the DB. I ran the following Command in the nuget package manager console.

Scaffold-DbContext "Server=localhost;Database=FlexDB;uid=root;pwd=ffff;Trusted_Connection=True;" MySql.Data.EntityFrameworkCore -OutputDir EFModels/DB

It unfortunately produces the following error.

Startup project 'FlexData' 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.

UPDATE-------------

I have followed the instruction from ChrFin but I now get the following error

Could not load type 'Microsoft.EntityFrameworkCore.Infrastructure.DesignTimeProviderServicesAttribute' from assembly 'Microsoft.EntityFrameworkCore, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

Blimey it was easier with an EDMX file and template!!!!!

1 Answer 1

1

To run such commands you need a library targeting a runtime (Full or core framework).
Just create an additional project, e.g. a console project targeting .NET 4.5 or newer, and a reference to your .NET standard library.
After setting the new project as the startup project you can then run the command against your original project and get the DB scaffolded in there.

Sign up to request clarification or add additional context in comments.

18 Comments

Will that not create the Entities in the Runtime Project?
@coolblue2000 I honestly don't know. Just try it and either move it manually afterwards or check if there is a parameter to change that. Anyway there is no way around such a "helper-project" as the PMC needs to know which runtime to use...
The project targeting the .NET Standard project only needs to be the startup project. You can still scaffold in the .NET Standard project by running the command targeting that project.
Thanks, I will give it a try. I assume this means that the .net Standard project is then no longer a "standard" project that can be used with other .net versions?
@coolblue2000 Yes, it is. You keep the current .NET Standard project with Entity Framework. You add an executable project (console, web, ...) that you set as the startup project (which also holds the connectionstring in its config). Then you run the scaffold command from the Package Manager Console targeting the .NET Standard project. That should do the trick.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.