Upgrade EF 4 EDMX to EF 6
Asked Answered
T

3

49

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.

Timework answered 21/10, 2013 at 14:6 Comment(0)
P
60
  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
Punchy answered 21/10, 2013 at 14:36 Comment(4)
You will want to rename or move any partial classes you have already created. My preference is to rename the file, e.g. from Person.cs to Person.partial.cs.Woodcock
Getting this error - No Entity Framework provider found for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. But my class library project's app.config already has provider with invariantName="MySql.Data.MySqlClient"?Spondaic
@HimalayaGarg - you should create a new questionPunchy
For error - No Entity Framework provider found, I found that provider with invariantName="MySql.Data.MySqlClient" should be there in both class library's app.config and website's web.config.Spondaic
S
30

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

Sonstrom answered 11/7, 2014 at 12:33 Comment(0)
S
13

As well as the steps Rand Random suggested. Remember to Install Entity Framework 6 Tools for Visual Studio 2012 if you are using VS2012. download here

This will ensure that EF 6.x DbContext Generator template shows when clicking Step 4: Add Code Generation Item

Skillless answered 1/11, 2013 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.