Toolkit
Toolkit

Reputation: 11119

Reusing EF entities for both EF 6.x and EF Core

I have a project on .NET 4.8 and EF 6.4.4. We are gradually migrating to .Net Core but during the process can I create a .NET Core data context class, EF Core and point both to same entities?

Upvotes: 3

Views: 691

Answers (1)

Artur
Artur

Reputation: 5531

Yes, you can. Refer to the diagram below.

  1. Convert all libraries to .netstandard2.0
  2. Start by moving all models to separate project.
  3. Then create new project where you are going to migrate your dal classes to EF Core (I'd recommend to keep the same namespace in Dal.csproj and Dal.Core.csproj so consumers won't be affected).
  4. Move your first dal class from Dal.csproj to Dal.Core.csproj. Now Stage + Commit - this is very important step, otherwise you will loose git history for this class. Now you can do all required changes to make it work with new DbContext
  5. Once you finish all migrations move everything from Dal.Core.csproj back to Dal.csproj and remove Dal.Core.csproj project. Do not forget to stage changes before commit so git will recognize moved files as rename rather than delete+add.

Tip: make sure you are aware of Cartesian Explosion Problem in EF Core 3

Note: if you are working in Model First approach with EDMX file that's not supported in EF Core, you can't convert the project with EDMX to netstandard2.0 and should keep it as net48.

enter image description here

Upvotes: 1

Related Questions