dotnet / dotnet/EntityFramework.Docs
EF6 to EF Core porting documentation outlines broken migration procedure
- Dominant language
- Mermaid
- Stars
- 1.7k
- Forks
- 2k
- Avg merge
- 7d 23h
- Merged PRs (30d)
- 16
Description
The guide for porting from EF6 to EF Core mentions the possibility of migrating by using the existing database and removing the actual migration code from a new initial migration. From my personal attempt at doing this I'm however running into the following issues:
1. Table names in EF6 are based on type, in EF Core on DbSet property name
2. Primary keys and foreign keys in EF6 include `dbo.` in the constraint names (EF6: `PK_dbo.Users`, EF Core: `PK_Users`)
3. Foreign keys without foreign key property in EF6 generate names with underscore prefixes before the referenced table key column name, in EF Core the additional underscore is not there
4. Index names in EF Core include the table or entity name in the name (`IX_TeamMembers_UserId`), EF 6 uses the property name only (`IX_UserId`)
5. EF6 might have generated boolean columns as `[bit] NOT NULL DEFAULT 0`, EF Core omits the `DEFAULT 0` for new tables (initial migration, so all tables are new). The `DEFAULT 0` causes a constraint to be generated, which on migration down is removed by EF6 by looking up the constraint in `sys.default_constraints`, because the name is partially randomized. A new migration in EF Core that would remove the property would probably fail because the constraint is referencing the property
These are definitely not all. I think it's bad advice to tell people to migrate to EF Core like this, it will cause a lot of frustration when continuing development on the project that was migrated. Some of these issues can definitely be remedied using additional configuration in EF Core, which permits generation of a schema that is much closer to the original EF6 schema. However, when someone would remove that configuration to make use of EF Core defaults you'll again run into issues because you can't rename primary keys if a foreign key is referencing the table.
I think the only feasible approach is to ensure the EF Core model matches table names and column names from the EF6 model, creating a new database using EF Core migrations and copying the data (possibly inserting data with keys) from the old (EF6) to the new (EF Core) database.
---
#### Document Details
⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*
* ID: e1226698-6874-6f8f-f716-1974152243fa
* Version Independent ID: 9e7bdb7a-d3dc-03a2-99f6-78aa46cc8357
* Content: [Porting from EF6 to EF Core - Porting a Code-Based Model - EF](https://docs.microsoft.com/en-us/ef/efcore-and-ef6/porting/port-code)
* Content Source: [entity-framework/efcore-and-ef6/porting/port-code.md](https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/efcore-and-ef6/porting/port-code.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @ajcvickers
* Microsoft Alias: **avickers**
Contributor guide
Assessment
This issue has not been assessed yet.