Attempting to migrate from Entity Framework 5 to 6 or DataBase-First = Code-First + chart browser

I want to share the rake of the transition from EFW 5 to 6 in the case of DataBase-First.

I work closely with the Entity Framework from the cradle. Moved to it with Linq2SQL.
I did not see any special problems with transitions between versions. And in general - each transition lived up to expectations.
But today I was a little disappointed.

At the start, I have 6 pieces of DataBase-First repositories scattered around the VS 2012 solution on .NET 4.5. And I tried to transfer them to EFW 6.

So, what needs to be done and in what order.

1. Go to the NuGet manager (Manage NuGet Packages), open the Updates tab and install Entity Framework 6 (6.0.1)
This procedure will remove references to old assemblies and add two new assemblies to the project (EntityFramework.dll, EntityFramework.SqlServer.dll)
This must be done for each project where there is EFW. If all this eventually starts somewhere in another application, then it is necessary to make similar changes in app.config (web.config)

2. You will have a lot of compilation errors in your project. But do not rush to correct them. First, we replace the entity generator and, thereby, automatically correct some of the errors. If the project has already used generators, delete (search and delete * .tt).
Go to the chart view, press the right pedal and select “Add Code Generation Item” and select “EF 6.x DBContextGenerator”.
If you have installed VS 2012 (and not 2013), then you first need to install Entity Framework Tools 6

3. Now we fix compilation errors.
As a rule, errors are related to the fact that namespace has changed.
The pattern of changes in most cases is this: after System.Data, .Entity.Core is added.

Everything, the project is ready to launch.

And now about the rake.

1. Lost (transferred) a bunch of methods that can not be fixed in the code automatically.
For example:
Context.DeleteObject () is no more, now go to the desired DbSet and call Remove () there.

context.Delete(user); //было
context.Users.Remove(user); //стало


Context.AddTo *** is also gone now, go to the desired DbSet and call Add () there.
context.AddToUsers(user); //было
context.Users.Add(user); //стало


The DbSet.AddObject method has also disappeared, but here you can solve it by AutoCorrect [AddObject] => [Add]
Particularly advanced can write Extension.

2. The internal types have changed.
If earlier context.Connection was of type EntityConnection,
then now SqlConnection suddenly appeared there.
Which broke part of my code (the meaning of the code was to find out the connection string in the “narrow” places and do the necessary things on ADO.Net)

3. In general, the events in the context disappeared.
Those. now context.SavingChanges events are simply gone.

4. Classes that are generated now do not have the same functionality. I would say that they now do not differ from structures at all.
They are not even inherited from EntityObject.
UPD
The authors do not plan to maintain or develop Self-Tracking entities. Proof

5. There were two repositories based on different databases. They had tables with the same name. As a result, the repositories had classes with the same names but different namespaces. On Entity Framework 5, this was not a problem. But on code 6, the code compiled, but in runtime I caught an exception, something about the impossibility of resolving the name (these classes featured in the message from different namespaces)

Points 3 and 4 suggest that the DataBase-First approach was merged. All that remains is Code-First + the screen as a class diagram browser.

Well and fatality - after switching to 6.0.1, my code began to work more slowly.
Perhaps the fact is that other, more optimal queries began to be generated and they went past the indexes created for EFW 5.
But I did not understand the reasons anymore. Just postponed the transition to more free time.

Also popular now: