Quick Tip: EF Core 1.0 Reverse Engineer POCO Generator

If you're new to Entity Framework (EF) Core 1.0, you may have noticed the Reverse Engineer code-first extension doesn't work. Today, I show a quick tip on using the equivalent in EF Core 1.0.

Written by Jonathan "JD" Danylko • Last Updated: • Develop •
Laptop with code on the monitor

I've always loved the Entity Framework Reverse POCO Code-First Generator to create fast data layer applications.

I even use it as a component for building applications at "Ludicrous Speed."

Then I heard about EF Core 1.0 coming out. I decided to test it out and see if there was a way to create the entities using that extension.

No such luck! The extension didn't work too well with the new ASP.NET 1.0 Core architecture.

After some digging, I found out that there were sooooo many ways to do this, based on the changes to EF in the last year, that I tried to include a complete example of how to create the entities and DbContext using EF Core 1.0.

The good news is that Microsoft included their own POCO Code-First Generator in the Entity Framework commands. The bad news is that it's a round-about way of doing it, but it does work.

Keep in mind that if your database structure changes at all, you need to "re-gen" your entities again along with their relationships so keep your entities "nimble" so keep Data Migrations in mind when updating your entities. 

Start it up!

To get this working with your current project,

1. Add these dependencies and commands to your ASP.NET Core 1.0 project.json file (NOTE: Keep in mind...these settings are current as of March 28, 2016, so they should work).

"dependencies": {
  "EntityFramework.Commands""7.0.0-rc1-final",
  "EntityFramework.Core""7.0.0-rc1-final",
  "EntityFramework.MicrosoftSqlServer""7.0.0-rc1-final",
  "EntityFramework.MicrosoftSqlServer.Design""7.0.0-rc1-final",
  "EntityFramework.Relational""7.0.0-rc1-final"
},
"commands": {
  "ef""EntityFramework.Commands"
}

2. After you add these to your project, do an update-package to get the updated versions of each Entity Framework library.

update-package

3. Select your Project folder and drop out to a Command Prompt. I use the Productivity Power Tools for 2015 and use the Open Command Prompt to drop me into any folder in my project.

4. At the command line, type:

dnx ef dbcontext scaffold Server=localhost;Database=MyDatabaseName;Trusted_Connection=true; EntityFramework.MicrosoftSqlServer -o Models\EF

I liked adding an output folder to the end so I know where my entities and DbContext will reside after it's done.

If you look in the Models\EF folder, you should see a lot of entities along with your DbContext.

Also, if you want to see the options for the EF scaffolding, type:

dnx ef dbcontext scaffold

at your command prompt (from step #3) to see all of the options.

This should give you everything you need to have fresh, new entities and DbContexts for your application.

Did I miss a step? Post your comments below.

ASP.NET 8 Best Practices on Amazon

ASP.NET 8 Best Practices by Jonathan Danylko


Reviewed as a "comprehensive guide" and a "roadmap to excellence" with over 120 Best Practices for ASP.NET Core 8, Jonathan's first book by Packt Publishing explores proven techniques for every phase of the SDLC.

Learn industry-standard concepts to improve your coding, debugging, and deployment of ASP.NET Core websites.

Order now on Amazon.com button

Picture of Jonathan "JD" Danylko

Jonathan "JD" Danylko is an author, web architect, and entrepreneur who's been programming for over 30 years. He's developed websites for small, medium, and Fortune 500 companies since 1996.

He currently works at Insight Enterprises as an Architect.

When asked what he likes to do in his spare time, he replies, "I like to write and I like to code. I also like to write about code."

comments powered by Disqus