防止根据多对多约定创建索引 table
Prevent index created by convention on many-to-many table
在下面的配置中,EF 按照惯例在 SyntaxId
上创建索引。由于我有一个复合主键(用作索引)并且没有标识列,因此我认为在多对多 table.
中不需要在单个列上按约定创建的索引
如何防止自动创建此约定索引 (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; }
}
事实证明,这是 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);
}
因为我的所有多对多实体都在同一个 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);
}
在下面的配置中,EF 按照惯例在 SyntaxId
上创建索引。由于我有一个复合主键(用作索引)并且没有标识列,因此我认为在多对多 table.
如何防止自动创建此约定索引 (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; }
}
事实证明,这是 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);
}
因为我的所有多对多实体都在同一个 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);
}