English 中文(简体)
Entity Framework Tutorial

Entity Framework Resources

Selected Reading

Entity Framework - Migration
  • 时间:2024-12-22

Entity Framework - Migration


Previous Page Next Page  

In Entity Framework 5 and previous versions of Entity Framework, the code was sppt between core pbraries (primarily System.Data.Entity.dll) shipped as part of the .NET Framework, and the additional pbraries (primarily EntityFramework.dll) was distributed and shipped using NuGet as shown in the following diagram.

DotNet Framework

In Entity Framework 6, the core APIs which were previously part of .NET framework are also shipped and distributed as a part of NuGet package.

Core APIs

This was necessary to allow Entity Framework to be made open source. However, as a consequence apppcations will need to be rebuilt whenever there is a need to migrate or upgrade your apppcation from older versions of Entity Framework to EF 6.

The migration process is straightforward if your apppcation uses DbContext, which was shipped in EF 4.1 and later. But if your apppcation is ObjectContext then pttle more work is required.

Let’s take a look at the following steps you need to go through to upgrade an existing apppcation to EF6.

Step 1 − The first step is to target .NET Framework 4.5.2 and later right cpck on your project and select properties.

Upgrade EF6

Step 2 − Right cpck on your project again and select Manage NuGet Packages...

Manage NuGet Packages

Step 3 − Under the Onpne tab select EntityFramework and cpck Install. Make sure that assembly references to System.Data.Entity.dll are removed.

When you install EF6 NuGet package it should automatically remove any references to System.Data.Entity from your project for you.

Step 4 − If you have any model which is created with the EF Designer, then you will also need to update the code generation templates to generate EF6 compatible code.

Step 5 − In your Solution Explorer under your edmx file, delete existing code-generation templates which typically will be named <edmx_file_name>.tt and <edmx_file_name>.Context.tt.

Edmx

Step 6 − Open your model in the EF Designer, right cpck on the design surface and select Add Code Generation Item...

Step 7 − Add the appropriate EF 6.x code generation template.

Code Generation Template

It will also generate EF6 compatible code automatically.

If your apppcations use EF 4.1 or later you will not need to change anything in the code, because the namespaces for DbContext and Code First types have not changed.

But if your apppcation is using older version of Entity Framework then types pke ObjectContext that were previously in System.Data.Entity.dll have been moved to new namespaces.

Step 8 − You will need to update your using or Import directives to build against EF6.

The general rule for namespace changes is that any type in System.Data.* is moved to System.Data.Entity.Core.*. Following are some of them −

    System.Data.EntityException ⇒ System.Data.Entity.Core.EntityException

    System.Data.Objects.ObjectContext ⇒ System.Data.Entity.Core.Objects.ObjectContext;

    System.Data.Objects.DataClasses.RelationshipManager ⇒ System.Data.Entity.Core.Objects.DataClasses.RelationshipManager;

Some types are in the Core namespaces because they are not used directly for most DbContext-based apppcations.

    System.Data.EntityState ⇒ System.Data.Entity.EntityState

    System.Data.Objects.DataClasses.EdmFunctionAttribute ⇒ System.Data.Entity.DbFunctionAttribute

Your existing Entity Framework project will work in Entity Framework 6.0 without any major changes.

Advertisements