Reverse engineer database to custom Code First entity model and mappings

Home/.Net, Entity Framework/Reverse engineer database to custom Code First entity model and mappings

It’s really cool to see how far code-first support within Entity Framework has come over the last few releases. It’s extremely freeing to be create our own POCOs and custom mapping classes, which nostalgically remind of past projects with FluentNHibernate. And we aren’t tied down to edmx files (and nasty merge conflicts that come with them)!

With that freedom comes a bit of extra responsibility on our part…meaning we have to actually create those POCOs, mappers, and DbContext classes. It turns out, the Entity Framework team has created some tools to make that work a bit easier for us if we already have a database schema in place. The latest release of EF Power Tools includes a ‘Reverse Engineer Code First’ utility to generate these initial classes.

First, we need to install the latest version of the EF Power Tools VS extension using the Tools –> Extensions and Updates menu item within Visual Studio.

EF Power Tools install

After installing, projects will have two new context menu items available within Visual Studio.

EF Power Tools Project Context Options

Reverse Engineer Code First – Given a database connection, creates the related models, mappers, and db context classes.
Customize Reverse Engineer Templates – Allows you to tailor the generation of these classes.

Let’s begin by reverse engineering a simple BookStore database containing two tables: Authors and Books. Upon selecting, we’re asked for database connection information which, once entered, will be used to generate those classes.

EF Database Connection

Depending on the size of your database, this step could take up to a couple minutes. Below are the classes generated.

EF Solution Explorer

Let’s take a look at one of the mappers.

public class BookMap : EntityTypeConfiguration<Book>
    public BookMap()
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.Title)

        // Table & Column Mappings
        this.Property(t => t.Id).HasColumnName("Id");
        this.Property(t => t.Title).HasColumnName("Title");
        this.Property(t => t.AuthorId).HasColumnName("AuthorId");

        // Relationships
        this.HasRequired(t => t.Author)
            .WithMany(t => t.Books)
            .HasForeignKey(d => d.AuthorId);

The generated code isn’t bad, but I’d like it to be a little cleaner. Luckily, we can modify how these are created by tweaking the T4 templates used to generate our model. The remaining cleanup can be done using Resharper. Let’s delete the Models folder and start over.

Right-click the VS project and select Entity Framework –> Customize Reverse Engineer Templates. If you receive the following prompt, click OK.

EF warning

Open Solution Explorer, and you should see the following T4 templates added to the project. (You can ignore the errors that VS says exists in the templates, they won’t actually stop the project from building…and we’ll be deleting the templates after generating our model).

I made the following changes to the generated templates:

  • Removed ‘partial’ keyword from entity and context classes
  • Removed ‘this’ qualifier throughout
  • Combined property mapping configuration

Now, we can run the Reverse Engineer tool again and this time we’ll get a slightly cleaner BookMap class.

public class BookMap : EntityTypeConfiguration<Book>
    public BookMap()
        HasKey(t => t.Id);

        Property(t => t.Id)

        Property(t => t.Title)

        Property(t => t.AuthorId)

        HasRequired(t => t.Author)
            .WithMany(t => t.Books)
            .HasForeignKey(d => d.AuthorId);

We didn’t do it here, but we could have modified the templates for other reasons as well. For example, we could have made all of our entities (or mappers) inherit from a common base class. Or we could have appended a ‘Dto’ suffix to the end of our model classes.

These templates are available on github.

About the Author:

Freelance software developer in the Cleveland area.

Leave A Comment