Setup Entity Framework v6.x

Possibly save 4 hours of your time: This is a brief introduction of setting your project to use Entity Framework v6.

  • Install Entity Framework
  • Create DbContext derived class
  • Create Entity model
  • Add connectionString to configuration
  • Write functional test

Install Entity Framework

In Visual Studio, make sure your project solution is open. Now go to Tools >> NuGet Package Manager >> Manage NuGet Packages for solution…

Select Browse >> then search for EntityFramework. The latest version is v6.2.0 at this time of writing for .NET Framework 4.5.2.

Then check the checkbox for the projects in your solution you want to install. Then click Install.

Once installed, you should see the following references:

  • EntityFramework
  • EntityFramework.SqlServer
  • System.ComponentModel.DataAnnotations

Create DbContext derived class

Next, you need to create a class that inherits from System.Data.Entity.DbContext.

Then create a constructor with the name of the connection string used in the configuration later.

Next you will need to create a property which is a System.Data.Entity.DbSet<entity model> that represents your database table. DbSet is like a collection of the entity models.

public class SomeNameContext : DbContext
  public SomeNameContext() : base("name=SomeNameContext")

  public virtual DbSet<YourEntityName> YourEntitiesName { get; set; }

Create Entity model

You will then create the partial class for the entity model. The model will need a Table attribute with the name of the table as argument.

When you create the properties, you can specify other property attributes such as:

  • Key – represents primary key
  • Column(Order = 1) – must specify column order if multiple keys exist
  • StringLength(30) – limit to 30 characters

 public partial class YourEntityName
  [Column(Order = 1)]
  public int Year { get; set; }

  [Column(Order = 2)]
  public string CountryCode { get; set; }

  public double Value { get; set; }

Add connectionString to configuration

In the project web or application configuration (web.config or app.config) file add the connectionStrings section if it is not already there and add an element for your connection string.

<add name="SomeNameContext" connectionString="data source=localhost;initial catalog=YourDatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

Write functional test

Using NUnit.Framework, we can test with the following:

public class AlbertaExportValueTests
public void Test()
using (var context = new ExportToolContext())
var albertaExports = context.AlbertaExportValues.ToList();
Assert.That(albertaExports.Count, Is.EqualTo(260702));


Programming Entity Framework: Code First: Creating and Configuring Data Models from Your Classes


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s