Entity Framework (1/1)

Introduction

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].

Prerequisites

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.

Workflows

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"?>
<configuration>
  <configSections>
    <!-- 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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

References

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

blog comments powered by Disqus

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

Home
Contact Us
Search