Entity Framework Model Mapping

The EntityTypeConfiguration class

Entity Framework Model Mapping
Photo by fabio on Unsplash

Entity Framework 6 is pretty awesome, and there is probably a part of it that you are not fully aware of.

Let me introduce you to the EntityTypeConfiguration class.

With this class you can separate your model mappings from your DbContext.

Here is an example of the folder structure that works with this strategy.

Here is a simple step by step example of how to implement Entity Framework Model Mapping

Create your model

Set up your DbContext

Setup your mapping using the EntityTypeConfiguration class.

Now we are ready to go!

Here is a UserService class that will use our configured context.

Piece of cake, right?

You can use this style for any type of model. You can also add associations to models, but I shall save that for another day!