How To Set Up Entity Framework 6 In ASP.NET MVC

In an effort to give back to this awesome community, I’m going to write on things that I’ve recently learned, and give ways to fix the problems that I ran into along the way. Without further ado, here goes…

Recently, I was working with fellow Improving Enterprises employee Nick Hunter on our MVC project, and had decided to use Entity Framework for our data storage and retrieval. We had used an MVC template in Visual Studio to start our project, which set us up with EF 5. As part of the Model View Controller (MVC) style, when properties are created in the model, Entity Framework automagically migrates that data into columns on a table that it creates. The way you tell EF what model to do this to is create a class that inherits from DbContext, and create DbSet properties.

As an example, say you’re making an MVC project that should list restaurants with location information, and also reviews for those restaurants with details. These two pieces of data should be in two different models and tables to keep everything organized. In this case, there would be a class called Restaurant and another called RestaurantReview in the Models folder. The Restaurant model would have properties for Name, Address, Phone Number, and possibly Category, among other things. The RestaurantReview model would have properties for ID that would refer to a restaurant, Rating, Details and others. Then a class should be created for the database context, so let’s call that PlacesToEatDb (I’m not great at naming things :P) and have it inherit from DbContext. Then set the following properties:

public DbSet Restaurants { get; set; }
public DbSet RestaurantReviews { get; set; }

After all that is done, new-up your PlacesToEatDb in your controller, and you’ll have a database that EF created to work with.

If you’re using current technology, this database was created in a new developer-focused database called a LocalDb. This is slightly different than SQL Express, in that it doesn’t run as a service; it runs on demand when you need it. When I first fired this up, it actually wasn’t working. I had to go to a command prompt and type SQLLocalDb Start v11.0, and then was able to connect to it.

Once you connect, you’ll notice that EF created two tables (named the same as the DbSets you created earlier) that already has columns populated (named the same as the properties in the models). If you want to have EF automatically change this whenever you add another property to the models, you’ll have to enable automatic migrations. This can be done by accessing the console in Visual Studio by selecting Tools->Nuget Package Manager->Package Manager Console. In this console, type Enable-Migrations –EnableAutomaticMigrations. You’ll then notice that a Migrations folder is created with a Configuration file in it. If you open this file, you’ll notice an EnableAutomaticMigrations flag. If everything was done correctly, migrations should automatically work when you add a new property to one of your models. If not, make sure that EnableAutomaticMigrations flag is set to true.

Also, if you wanted two different databases, you would create two different classes that inherit from DbContext. You can still have automatic migrations, but would need to specify -ContextTypeName when doing so. As an example, let’s say our project name is called PlacesToEat, and you created a different context for both the Restaurants and the RestaurantReviews. Your command would look like this: Enable-Migrations -ContextTypeName PlacesToEat.RestaurantContext.Configuration and Enable-Migrations -ContextTypeName PlacesToEat.RestaurantContext.Configuration (NOTE: for this path, make sure to use the periods instead of slashes. Slashes will not work.).

On some of these steps, we seemed to have ran into some glitches. To fix, we used Nuget to install EF 6 over EF 5. I wouldn’t recommend trying to uninstall it first, as the install of EF 6 takes care of this. Also, the uninstall will error out anyway, saying that some things depend on EF.

Well, that’s all I have for now. If you’d like more information, K. Scott Allen does a great job of explaining it in his Pluralsight course, which is currently free to view. Link:

Thanks for reading!