在多对多表上防止由约定创建的索引

时间:2018-02-06 14:57:33

标签: c# mysql ef-code-first entity-framework-core ef-fluent-api

在下面的配置中,EF按惯例在SyntaxId上创建索引。由于我有一个复合主键(用作索引)而没有标识列,因此我不认为在多对多表中需要在单个列上创建这种约定创建的索引。

如何防止自动创建此约定索引(b.HasIndex("SyntaxId");)?

public class SoftwareSyntaxTypeConfiguration : BaseJunctionTypeConfiguration<SoftwareSyntax>
{
    public override void Configure(EntityTypeBuilder<SoftwareSyntax> entityTypeBuilder)
    {
        base.Configure(entityTypeBuilder);
        entityTypeBuilder.ToTable("software_syntaxes");
        entityTypeBuilder.HasKey(x => new {x.SoftwareId, x.SyntaxId});
        entityTypeBuilder.HasOne(x => x.Software)
                         .WithMany(x => x.SoftwareSyntaxes)
                         .HasForeignKey(x => x.SoftwareId);
        entityTypeBuilder.HasOne(x => x.Syntax)
                         .WithMany(x => x.SoftwareSyntaxes)
                         .HasForeignKey(x => x.SyntaxId);
    }
}

partial class FilterListsDbContextModelSnapshot : ModelSnapshot
{
    protected override void BuildModel(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity("FilterLists.Data.Entities.Junctions.SoftwareSyntax", b =>
            {
                b.Property<int>("SoftwareId");

                b.Property<int>("SyntaxId");

                b.HasKey("SoftwareId", "SyntaxId");

                b.HasIndex("SyntaxId"); //TODO: prevent this from being auto-created

                b.ToTable("software_syntaxes");
            });
    }
}

更新:添加实体类以便澄清。

public class Software
{
    public int Id { get; set; }
    public ICollection<SoftwareSyntax> SoftwareSyntaxes { get; set; }
    ...
}

public class Syntax
{
    public int Id { get; set; }
    public ICollection<SoftwareSyntax> SoftwareSyntaxes { get; set; }
    ...
}

public class SoftwareSyntax
{
    public int SoftwareId { get; set; }
    public Software Software { get; set; }

    public int SyntaxId { get; set; }
    public Syntax Syntax { get; set; }
}

1 个答案:

答案 0 :(得分:0)

事实证明,这是not officially supported yet

但是,解决方法是使用内部EF API。它的使用带有警告,可以在不另行通知的情况下进行更改,因为它不适合外部使用。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    ((Model)modelBuilder.Model).ConventionDispatcher.StartBatch();
    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    foreach (var index in entityType.GetIndexes().ToList())
        // if index is one you want to remove
            entityType.RemoveIndex(index.Properties);
}

via Arthur Vickers

由于我的所有多对多实体都在同一个Junctions命名空间中,因此目前适合我的实现是:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    ((Model)modelBuilder.Model).ConventionDispatcher.StartBatch();
    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    foreach (var index in entityType.GetIndexes().ToList())
        if (index.DeclaringEntityType.Name.Contains("Junctions"))
            entityType.RemoveIndex(index.Properties);
}