Nuget Package: https://www.nuget.org/packages/EntityFramework.DiscriminatorIndex/
GitHub: https://github.com/marcusmiris/EntityFramework.DiscriminatorIndex
Setup
Migration Configuration file:
internal sealed class Configuration : DbMigrationsConfiguration{ public Configuration() { this.AddSupportToDiscriminatorIndex(); } }
DbConfiguration:
public class DbConfiguration : System.Data.Entity.DbConfiguration { public DbConfiguration() { SetMetadataAnnotationSerializer( DiscriminatorIndexAnnotation.AnnotationName, () => new DiscriminatorIndexAnnotationSerializer()); } }
How To Use?
Entity Type Configuration
To configure a Discriminator index on an entity, we can use the extention method HasIndexOnDiscriminator() of the EntityTypeConfiguration:
public class DbContext: System.Data.Entity.DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity().HasIndexOnDiscriminator(); base.OnModelCreating(modelBuilder); } }
Optionally, we can customize the column name, or also set the index name.
modelBuilder.Entity().HasIndexOnDiscriminator( columnName: "MyDiscriminator", indexName: "IX_Entity_Discriminator");
Convention
Using the convention, all columns named as "Discriminator" in the database will be indexed.public class DbContext: System.Data.Entity.DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Add(); base.OnModelCreating(modelBuilder); } }
Optionally, we can provide an alternative column name to search for:
modelBuilder.Conventions.Add(new DiscriminatorIndexConvention(
discriminatorColumnName: "CustoDiscriminator"));When using the convention, we can also ignore some entity:
modelBuilder.Entity() .HasTableAnnotation(DiscriminatorIndexAnnotation.AnnotationName, null);
... or yet replace the default convention using the HasIndexOnDiscriminator extension method.
Nenhum comentário:
Postar um comentário