Upgrade EF 4 EDMX to EF 6


Question

My application is using a database first EDMX in EF 4. I would like to upgrade everything to EF 6. After getting EF 6 with NuGet I had to make a lot of changes to my classes that are using my EF model, because namespaces have been changed in EF 6. Then I realized, that the code generated by my EDMX does also use the wrong namespaces etc. I'm not using a custom T4 so far.

How would I upgrade my existing EDMX to EF 6.

Thank you.

Accepted Answer

  1. You delete your old .tt files
  2. You open your edmx file in designer mode (so you can see your model)
  3. Right click on a free space
  4. Select Add Code Generation Item
  5. In the dialog select "EF 6.x DbContext Code Generation Item" (something like this)
  6. Save your edmx and all classes will be generated for you, with the new namespaces and so on

Popular Answer

In addition to the answers given here by Rand Random and Dean Oliver, let me mention the following MSDN link, describing general steps for upgrading to EF6. Don't underestimate the manual steps required...

The road map is (see details in the link given above):

  1. Preparation: Install the Entity Framework 6 Tools for Visual Studio 2012/13

  2. Install the EF6 NuGet package

  3. Ensure that assembly references to System.Data.Entity.dll are removed (Note: Installing the EF6 NuGet package should automatically remove any references to System.Data.Entity from your project for you).

  4. Swap any EF Designer (EDMX) models to use EF 6.x code generation.

    Notes:

    • If you're getting the message "The Entity Data Model Designer is unable to display the file you requested" afterwards, then click on the link modify in the displayed text message "The entity Data Model Designer ... You can modify ...", which will display the tables. Select all with Ctrl+A, then press Del, then right-click and select "Update model from database", and finally save using Ctrl+S. This will update the model to the latest version using the default T4-Template "EF 6.x DbContext Generator".

    • If you have used ObjectContext in your project, then you should consider downloading the template "EF 6.x EntityObject Generator". Then right-click in the model designer, choose "Add code generation item", then choose a name you haven't used yet. It will generate the right classes, afterwards you have to remove all old ("*.tt") files and related generated class ("*.cs") files.

  5. Update namespaces for any core EF types being used, i.e.

    • any type in System.Data.* is moved to System.Data.Entity.Core.*
    • System.Data.EntityState => System.Data.Entity.EntityState
    • System.Data.Objects.DataClasses.EdmFunctionAttribute => System.Data.Entity.DbFunctionAttribute.
      Note: This class has been renamed; a class with the old name still exists and works, but it is now marked as obsolete.
    • System.Data.Objects.EntityFunctions => System.Data.Entity.DbFunctions.
      Note: This class has been renamed; a class with the old name still exists and works, but it is now marked as obsolete.
    • Spatial classes (e.g. DbGeography, DbGeometry) have moved from
      System.Data.Spatial => System.Data.Entity.Spatial

N.B.:





Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why