Introduction to the Entity Framework 6


In this blog I will show you how to quickly store and retrieve data in your existing project using the Entity Framework. As example project I will use an existing EPiServer project.

Enabling Entity Framework in your project

Start with adding the Entity Framework references to your project. Right-click on References and choose Manage NuGet Packages. Search for Entity Framework package and install it.

NuGet Package Manager for installing Entity Framework 6

NuGet Package Manager for installing Entity Framework 6

Alternatively use the Package Manager Console and type in:
PM> Install-Package EntityFramework

Creating a model

Now lets say we have a webshop and we want to store a webshop order in the database using the Entity Framework. For that lets create two models and put them in ~/Models/EntityFramework or any other directory you prefer.

And a model for the products you ordered.

Some cool attributes that you can use in your models are described here:

Now lets have a brief look at those models. Most of the properties are self explaining but there is one special property which is the virtual property. This is used for lazy loading. Meaning that if you load an order from the database you get all related products in the same query for free. And by free I mean without doing extra coding or requests.

Now we need one more thing. Because this is just a model and it still needs to be connected (mapped) to a database. For that we use a Database Context.

The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the System.Data.Entity.DbContextclass. In your code you specify which entities are included in the data model. You can also customize certain Entity Framework behavior.

Lets create a simple database context for our order models.

As you can see it doesnt do much more then asking you which database to use by requiring a connectionstring in the constructor and telling it to store Order and Product models.

Storing data using the Entity Framework

Now lets pretend we just received a new order through the submission of a form on your webshop.

I understand this is a lot of code. But lets take a look at the Entity Framework specific code. The code belongs to an EPiServer project in which we already have a connectionstring EPiServerDB configured.

We want the Entity Framework to use the same database. Ofcource by passing a different connectionstring you can also save your EF models to a different database.

I think the rest of the code is pretty self explaining. You save the order and then save all individual products you bought. The string shoppingcart is a JSON serialized object containing which product you bought (every product has its own content page and therefor is related to a product page), how many times you want this specific product and what its price is.

Retrieving your orders from the database using Entity Framework

This is also very simple. All you need to do is something like this:

This will return you order with ID 5.

Quick tip when you accidently deleted your local database file (.mdf)

If you like to mess around like me you probably have tried once to delete the mdf file and see what happends. I expected deleting the file would just reset my database back to an empty one but instead the site kept complaining about ‘Cannot attach the file ‘.mdf’ as database in MVC‘. The reason is if you delete the DB file, it still stays registered with SqlLocalDB. You can fix it by deleting you localDB like this: (run from your console)

Now restart your app or run ‘update-database’ and the database file should be recreated.

Leave a Comment.