-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set MaxLength on TPH discriminator property by convention #10691
Comments
@satyajit-behera I just sent a PR to update the docs for it: dotnet/EntityFramework.Docs#601. Here's the update: Configuring the discriminator propertyIn the examples above, the discriminator is created as a shadow property on the base entity of the hierarchy. Since it is a property in the model, it can be configured just like other properties. For example, to set the max length when the default, by-convention discriminator is being used: modelBuilder.Entity<Blog>()
.Property("Discriminator")
.HasMaxLength(200); The discriminator can also be mapped to an actual CLR property in your entity. For example: class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasDiscriminator<string>("BlogType");
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public string BlogType { get; set; }
}
public class RssBlog : Blog
{
public string RssUrl { get; set; }
} Combining these two things together it is possible to both map the discriminator to a real property and configure it: modelBuilder.Entity<Blog>(b =>
{
b.HasDiscriminator<string>("BlogType");
b.Property(e => e.BlogType)
.HasMaxLength(200)
.HasColumnName("blog_type");
}); |
Thanks. This fixes for each entity individually. Is there any way where I can set it for all the discriminators across Context in one setting. |
@satyajit-behera Run through all the entity types at the end of OnModelCreating and add configuration. For example, something like this: foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
entityType.FindProperty("Discriminator")?.SetMaxLength(200);
} |
@ajcvickers Thanks a lot |
I couldn't set the max Length using the Thats why I added the configuration right after with the following code:
|
Note from triage: consider using an approach like shown here: dotnet/EntityFramework.Docs#4000, possibly using power-of-two buckets to avoid a migration on every change to discriminator value length. |
The Discriminator column in TPH is created nvarchar(max) by default.
Can we set it to a fixed length (say nvarchar(200)) and index it globally with one setting.
Thanks
The text was updated successfully, but these errors were encountered: