Entity Framework (1/1)


Entity Framework is Microsoft’s recommended data access technology for new applications. It is an object-relational mapper [1] that enables .NET to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write [2].


In this tutorial we will use LocalDB as the database we want to work with. If you are not familiar with LocalDB please have a look at our LocalDB tutorial.


There are 3 different workflows that can be used with the Entity Framework to access a relational database from your .NET application.

In this tutorial we explain the "Code first" approach.

A Simple Example

We will start with a simple example that stores a list of users in a LocalDB instance.

We use Visual Studio 2013 to create a C# Console Application called "EntityFramework1" as shown in Figure 1.

Project Creation Dialog
Figure 1: Project Creation Dialog

We then get the Entity Framework NuGet package as shown in Figure 2. In this tutorial we use version 6.1.

EntityFramework NuGet Package
Figure 2: EntityFramework NuGet Package

Installing the Entity Framework NuGet package adds the necessary references to the project but also modifies App.config as shown below.

File: App.config
<?xml version="1.0" encoding="utf-8"?>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
        EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameter value="v11.0" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />


  1. Wikipedia: Object-relational mapping
  2. MSDN: Entity Framework

blog comments powered by Disqus

Copyright(c) 2006-2017 Xavier Leclercq | Privacy policy

Contact Us