dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.58k stars 1.94k forks source link

Document discriminator column max length changes in EF8 #4655

Closed AminEsmaeily closed 2 months ago

AminEsmaeily commented 3 months ago

Current Statement

In one of our applications, we have a THP structure for some of our models. In the previous versions, EF added the Discriminator shadow property to our models automatically and its length has been set to Max.

Problem Statement

By upgrading the EF and the .Net version to 8, by adding a new database migration using the following command, EF adds an update to the Discriminator to shrink its length: dotnet ef migrations add NewMigrationName

image

After adding the migration, when we want to apply it to the database using dotnet ef database update, it shows the following error message:

The index 'dta_index_organizations' is dependent on column 'Discriminator'. ALTER TABLE ALTER COLUMN Discriminator failed because one or more objects access this column.

The mentioned index in the above error message is also generated by EF automatically. We manually added the Discriminator column to the models without setting the MaxLength for it and followed the different ways of configuring the Discriminator columns mentioned here to bypass the automatic updates, but nothing changed.

Provider and Version Information

EF Core version: 8.0.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 8.0 Operating system: MS Windows 10 IDE: Visual Studio 2022 17.8.6

ajcvickers commented 3 months ago

@AminEsmaeily You can configure the max length as unspecified again like this:

modelBuilder.Entity<Foo>()
    .Property<string>("Discriminator")
    .HasMaxLength(-1);

Note for team: the index exception is unfortunate here. We don't create indexes for discriminators by default, but an index will be created if the discriminator is part of a key.

AminEsmaeily commented 3 months ago

Thank you @ajcvickers , the issue is fixed now