Upgrade from Entity Framework 5 to 6
Asked Answered
B

4

43

After upgrading our project from using Entity Framework 5 to Entity Framework 6 (though NuGets update function) i get the following error on my generated Entities class:

Error 1 The type or namespace name 'Objects' does not exist in the namespace 'System.Data'
(are you missing an assembly reference?)

I understand that this is because the namespace has changed and i can manually fix the error by changing my imports from: using System.Data.Objects; and using System.Data.Objects.DataClasses; To: using System.Data.Entity.Core.Objects;

However the file is generated so i need to reapply this fix after every Update model from Database. Is there something extra to change to get EF to generate the model without this error.

Buckley answered 20/2, 2014 at 2:27 Comment(2)
See this link msdn.microsoft.com/en-us/library/upgradeef6.aspxMayfield
@KimKiWon Unfortunately that link no longer works but I expect it refers to this article, or something similar: learn.microsoft.com/en-us/ef/ef6/what-is-new/upgrading-to-ef6Saltwort
A
68

I think your problem is, that your T4 templates, which generate the entitties and the context are still in EF version 5.

First you have to delete the current code generation items, which are in the code behind of the model, namely <Modelname>.Context.tt and <Modelname>.tt.Next add a new EF version 6 code generator with Right click in the model designer-> Add Code Generation Item ... -> EF 6.x DbContext Generator.

Addressograph answered 20/2, 2014 at 5:38 Comment(3)
For me it worked after i changed my advanced system settings https://mcmap.net/q/390342/-metadata-file-not-found-data-entity-model and after that changed .tt file version as described in this post, thanks.Bicipital
That worked for me too but I didn't have the EF 6.x DbContext Generator object available until I installed the tools for VS2012. Get them from: microsoft.com/en-us/download/details.aspx?id=40762Footlight
I also had to delete <Modelname>.Context.csEyla
C
40

This is my experience on how to successfully upgrade Entity Framework v5 to v6 for:

  • SQL Server.
  • C# and Visual Studio 2012.
  • Database first.

Acronyms:

  • EF5 = Entity Framework v5.
  • EF6 = Entity Framework v6.

Checklist:

  1. EF5 is built into the core of .NET 4.5, whereas EF6 has been shifted out, and is open source.
    • This means that you must add the new EF6 assemblies to all of the relevant projects in the solution, in particular the entry project.
    • This means that you must remove assembly System.Entity from all projects, as this refers to EF5.
  2. EF5 has a single .dll "EntityFramework.dll", whereas EF6 has two .dlls:
    • EntityFramework
    • EntityFramework.SqlServer
  3. EF6 requires changes to app.config. The best way to make these changes is to right click on the Solution, select "Manage NuGet Packages for Solution", search for "EntityFramework" and install v6.1.0 of Entity Framework into all of the relevant projects, in particular the entry project. Make sure you uninstall any NuGet packages for EF5 Framework from all projects. This will automatically update your app.config files so they are correct.
  4. Examine all app.config files for references to EF5, and remove them.
  5. The namespaces have changed:
    • Remove C# lines using System.Data.EntityClient;, which is an EF5 reference.
    • Add C# line using System.Data.Entity.Core.EntityClient; which is the correct for EF6.

Still stuck? This checklist is a Community Wiki, feel free to edit this checklist to help other hapless souls who are still banging their heads against the brick wall that can be EF6 configuration.

Update 2016-02-15

Please explore other options before considering EF. It's 100x slower than other options, it's vastly over complicated for what it delivers, the entity GUI is full of bugs and has weird usability issues, and we are going to have to rip out all of our EF6 code and replace it with something that takes less than 5 minutes to make a query that takes 5 seconds in Dapper.

Chromatogram answered 20/2, 2014 at 2:27 Comment(3)
I am trying to upgrade a MVC3 project to MVC5. Upgrade went fine. However, stuck at code generation. Tried every instruction available on the Internet. Tried T4, Tried Legacy code generation. But it just wouldn't generate the code. Using EF 6.1.3. All references are in order. Nuget upgraded and updated all assemblies. Checked and rechecked config. Everything looks ok but no code generation. Is there a trace file or log file somewhere that I can peek into to see what is the issue?Mogerly
There is an option inside Visual Studio "Regenerate T4 templates". This will auto generate the C# from the T4 templates that Entity Framework generates. There is a known bug in Visual Studio which means that these T4 templates do not always auto generate their deoendent C# code if the T4 template changes, especially if the T4 template is shifted into a subfolder in Solution Explorer.Chromatogram
I was getting this error... An error occurred while applying transformation to 'App.config' ... The workround for this is to remove 5.0, rename the app.config file, remove the app.config from your VisualStudio project. With package manager add EF6 or whatever, then reconsile the new app.config it generates with the renamed one.Teplica
S
2

Microsoft have a page on upgrading to EF6: https://learn.microsoft.com/en-us/ef/ef6/what-is-new/upgrading-to-ef6

Note: This may be same content @Kim Ki Won shows above but that link no longer works.

Saltwort answered 1/10, 2020 at 13:33 Comment(0)
F
1

I know this is late to respond to the question but.

This work For me by following the below steps.

Copy the code form the existing abc.tt file. Delete the abc.tt file. Copy all the code again into ABC.tt file. After copy code just save the file and build the solution it will regenerate the complete .cs file as well

Frulla answered 23/4, 2020 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.