实体框架索引生成错误

我正在尝试创建 2 个这样的索引:


modelBuilder.Entity<MyEntity>()

    .HasIndex(p => new { p.Column1, p.Column2, p.Column3, p.Column4 })

    .HasName("ix_index1")

    .IsUnique();


modelBuilder.Entity<MyEntity>()

    .HasIndex(p => new { p.Column1, p.Column2, p.Column3 })

    .HasName("ix_index2")

    .IsUnique();

运行命令后add-migration InitialCreate,我得到的回报是这个脚本:


CreateTable(

    "DEV.MyEntity",

    c => new

        {

            Column1 = c.String(nullable: false, maxLength: 10),

            Column2 = c.Decimal(nullable: false, precision: 19, scale: 0),

            Column3 = c.Decimal(nullable: false, precision: 10, scale: 0),

            Column4 = c.Decimal(nullable: false, precision: 10, scale: 0),

        })

    .PrimaryKey(t => new { t.Column1, t.Column2, t.Column3 })

    .Index(t => new { t.Column1, t.Column2, t.Column3 }, unique: true, name: "ix_index2")

    .Index(t => t.Column4, unique: true, name: "ix_index1");

是否有原因ix_index1只Column4存在索引中的所有列?预期结果是ix_index14 列。


如果这是相关的,我正在使用托管 Oracle 数据库提供程序。实体框架版本 6.2。


DIEA
浏览 185回答 2
2回答

忽然笑

看起来像 EF6 错误(我猜是为了优化冗余索引)。数据库提供程序无关紧要(SqlServer 提供程序也是如此)。一种错误的行为是你所描述的。另一种情况是,如果您交换两次HasIndex调用的顺序,生成的迁移包含正确的ix_index1,但ix_index2根本没有。由于这是一个错误,除了将问题报告给 EF6 问题跟踪器并等待最终修复之外,别无他法。但是,如果(Column1, Column2, Column2)是迁移所指示的 PK&nbsp;,则该 PKix_index2是多余的,您可以安全地将其从 fluent 配置中删除,这将允许正确生成ix_index1.

MM们

好的,因为我最初尝试的方法被确认由于错误而被破坏 - 我能够找到另一个解决方案,它似乎工作正常,但更脏。例如,假设您要创建 4 个索引,如下所示:modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .HasIndex(p => new { p.Column1, p.Column2, p.Column3, p.Column5 })&nbsp; &nbsp; .HasName("ix_1")&nbsp; &nbsp; .IsUnique();modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .HasIndex(p => new { p.Column1, p.Column2, p.Column4, p.Column5 })&nbsp; &nbsp; .HasName("ix_2")&nbsp; &nbsp; .IsUnique();modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .HasIndex(p => new { p.Column1, p.Column2, p.Column3 })&nbsp; &nbsp; .HasName("ix_3")&nbsp; &nbsp; .IsUnique();modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .HasIndex(p => new { p.Column1, p.Column2, p.Column4 })&nbsp; &nbsp; .HasName("ix_4")&nbsp; &nbsp; .IsUnique();结果(截至撰写本文时 31.07.2018)将被破坏。要正确创建所有索引,您必须改用以下方法:modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .Property(e => e.Column1)&nbsp; &nbsp; .HasColumnAnnotation(&nbsp; &nbsp; &nbsp; &nbsp; IndexAnnotation.AnnotationName,&nbsp; &nbsp; &nbsp; &nbsp; new IndexAnnotation(new[]&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_3", 1) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_4", 1) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_2", 1) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_1", 1) { IsUnique = true }&nbsp; &nbsp; &nbsp; &nbsp; }));modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .Property(e => e.Column2)&nbsp; &nbsp; .HasColumnAnnotation(&nbsp; &nbsp; &nbsp; &nbsp; IndexAnnotation.AnnotationName,&nbsp; &nbsp; &nbsp; &nbsp; new IndexAnnotation(new[]&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_3", 2) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_4", 2) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_2", 2) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_1", 2) { IsUnique = true }&nbsp; &nbsp; &nbsp; &nbsp; }));modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .Property(e => e.Column3)&nbsp; &nbsp; .HasColumnAnnotation(&nbsp; &nbsp; &nbsp; &nbsp; IndexAnnotation.AnnotationName,&nbsp; &nbsp; &nbsp; &nbsp; new IndexAnnotation(new[]&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_3", 3) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_1", 3) { IsUnique = true }&nbsp; &nbsp; &nbsp; &nbsp; }));modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .Property(e => e.Column4)&nbsp; &nbsp; .HasColumnAnnotation(&nbsp; &nbsp; &nbsp; &nbsp; IndexAnnotation.AnnotationName,&nbsp; &nbsp; &nbsp; &nbsp; new IndexAnnotation(new[]&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_4", 3) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_2", 3) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; }));modelBuilder.Entity<MyEntity2>()&nbsp; &nbsp; .Property(e => e.Column5)&nbsp; &nbsp; .HasColumnAnnotation(&nbsp; &nbsp; &nbsp; &nbsp; IndexAnnotation.AnnotationName,&nbsp; &nbsp; &nbsp; &nbsp; new IndexAnnotation(new[]&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_2", 4) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IndexAttribute("ix_1", 4) { IsUnique = true },&nbsp; &nbsp; &nbsp; &nbsp; }));我希望我没有搞砸任何数字。但是这个想法应该是可以理解的。
打开App,查看更多内容
随时随地看视频慕课网APP