防止按约定在多对多表上创建索引 [英] Prevent index created by convention on many-to-many table
问题描述
在下面的配置中,EF 按照约定在 SyntaxId
上创建索引.由于我有一个复合主键(用作索引)并且没有标识列,因此我认为在多对多表中不需要在单个列上使用约定创建的索引.
In the configuration below, EF creates an index on SyntaxId
by convention. Since I have a composite primary key (serves as index) and no identity column, I do not think this convention-created index on a single column is needed in a many-to-many table.
如何防止自动创建此约定索引 (b.HasIndex("SyntaxId");
)?
How can I prevent this convention index (b.HasIndex("SyntaxId");
) from being auto-created?
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");
});
}
}
更新:添加实体类以进行说明.
Update: Adding entity classes for clarification.
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; }
}
推荐答案
事实证明,这是 尚未正式支持.
不过,一种解决方法是使用内部 EF API.它的使用需要注意的是,它可能会更改,恕不另行通知,因为它不适合外用.
A workaround, though, is to use an internal EF API. Its use carries the caveat that it could be changed without notice as it is not intended for external use.
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
命名空间中,因此目前对我有用的实现是:
Since I have all of my many-to-many entities in the same Junctions
namespace, the implementation that works for me currently is:
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);
}
这篇关于防止按约定在多对多表上创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!