EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

Document discriminator column max length changes in EF8

Open AminEsmaeily opened this issue 1 year ago • 2 comments

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

AminEsmaeily avatar Feb 12 '24 07:02 AminEsmaeily

@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.

ajcvickers avatar Feb 12 '24 10:02 ajcvickers

Thank you @ajcvickers , the issue is fixed now

AminEsmaeily avatar Feb 12 '24 11:02 AminEsmaeily