Thursday, 28 February 2019

EntityFramework Core -> scaffold POCO classes from DB into .Net Standard proj



  • Add Microsoft.EntityFrameworkCore.SqlServer from Nuget
  • From the Package Manager Console install entity frameworkcore tools
Install-Package Microsoft.EntityFrameworkCore.Tools
  • Have a project that targets Core or Framework as the startup project in the solution (or can use the –StartupProject ‘MyStartUpProject’ flag during the scaffold)
  • Tell EF to build the context and POCO classes
Scaffold-DbContext "Server=xxxxx;Database=xxxxxx;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer –OutputDir Models –Project Tdhb.MyStandardLib
  • Puts the ‘Models’ folder into the Tdhb.MyStandardLib project


So you change the database didn’t you. Now the POCOs are out of sync with the DB.
  • Run the scaffold command again but put –Force on the end to overwrite existing files
Scaffold-DbContext "Server=xxxxx;Database=xxxxxx;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project Tdhb.MyStandardLib -Force


If not having a startup project that targets Core of Framework then get the error (either add project (core or framework) and set as startup or use the StartupProject flog:

Startup project 'Tdhb.MyStandardLib' 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. For more information on using the EF Core Tools with .NET Standard projects, see https://go.microsoft.com/fwlink/?linkid=2034705

No comments:

Post a Comment