Skip to content

Migrate to EF Core #417

@sfiruch

Description

@sfiruch

If we changed from LINQ-to-SQL to EF Core, schema updates would become a whole lot easier. During application startup, we can run dbContext.Database.Migrate();, and the database schema will be at the current level. EF Core supports automatic up- and down-grading of schemas.

Here's how it would work

  1. Rip out LINQ-to-SQL (.dbml), etc.
  2. Use automated tooling to create a "EF Code First" model once, for example with https://marketplace.visualstudio.com/items?itemName=ErikEJ.EFCorePowerTools
  3. Run dotnet ef migrations add InitialCreate
  4. Make sure that the application executes dbContext.Database.Migrate(); at startup
  5. Enable lazy loading (https://learn.microsoft.com/en-us/ef/core/querying/related-data/lazy)

When we want to change the schema

  1. Edit the C# models
  2. Run dotnet ef migrations add AddAwesomeNewFieldToProjectTable
  3. Review the generated migration code to make sure it doesn't accidentally delete important data

Here's some documentation: https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli

Notes

  • Modern EF Core versions don't support .NET Framework 4.8 anymore (which we're using here). We should use EF Core 3.1 instead.
  • There's a difference between Entity Framework (EF), and Entity Framework Core. EF Core is a complete re-write of EF, and much newer.
  • This migration will be helpful if we eventually move this application to a modern .NET version.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions