代码之家  ›  专栏  ›  技术社区  ›  Alex

错误生成实体框架索引

  •  2
  • Alex  · 技术社区  · 6 年前

    我尝试创建两个这样的索引:

    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 出席了吗?预期结果是 IXI索引1 用于4列。

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

    2 回复  |  直到 6 年前
        1
  •  2
  •   Ivan Stoev    6 年前

    HasIndex ix_index1 ix_index2

    (Column1, Column2, Column2)

        2
  •  1
  •   Alex    6 年前

    modelBuilder.Entity<MyEntity2>()
        .HasIndex(p => new { p.Column1, p.Column2, p.Column3, p.Column5 })
        .HasName("ix_1")
        .IsUnique();
    
    modelBuilder.Entity<MyEntity2>()
        .HasIndex(p => new { p.Column1, p.Column2, p.Column4, p.Column5 })
        .HasName("ix_2")
        .IsUnique();
    
    modelBuilder.Entity<MyEntity2>()
        .HasIndex(p => new { p.Column1, p.Column2, p.Column3 })
        .HasName("ix_3")
        .IsUnique();
    
    modelBuilder.Entity<MyEntity2>()
        .HasIndex(p => new { p.Column1, p.Column2, p.Column4 })
        .HasName("ix_4")
        .IsUnique();
    

    modelBuilder.Entity<MyEntity2>()
        .Property(e => e.Column1)
        .HasColumnAnnotation(
            IndexAnnotation.AnnotationName,
            new IndexAnnotation(new[]
            {
                new IndexAttribute("ix_3", 1) { IsUnique = true },
                new IndexAttribute("ix_4", 1) { IsUnique = true },
                new IndexAttribute("ix_2", 1) { IsUnique = true },
                new IndexAttribute("ix_1", 1) { IsUnique = true }
            }));
    
    modelBuilder.Entity<MyEntity2>()
        .Property(e => e.Column2)
        .HasColumnAnnotation(
            IndexAnnotation.AnnotationName,
            new IndexAnnotation(new[]
            {
                new IndexAttribute("ix_3", 2) { IsUnique = true },
                new IndexAttribute("ix_4", 2) { IsUnique = true },
                new IndexAttribute("ix_2", 2) { IsUnique = true },
                new IndexAttribute("ix_1", 2) { IsUnique = true }
            }));
    
    modelBuilder.Entity<MyEntity2>()
        .Property(e => e.Column3)
        .HasColumnAnnotation(
            IndexAnnotation.AnnotationName,
            new IndexAnnotation(new[]
            {
                new IndexAttribute("ix_3", 3) { IsUnique = true },
                new IndexAttribute("ix_1", 3) { IsUnique = true }
            }));
    
    modelBuilder.Entity<MyEntity2>()
        .Property(e => e.Column4)
        .HasColumnAnnotation(
            IndexAnnotation.AnnotationName,
            new IndexAnnotation(new[]
            {
                new IndexAttribute("ix_4", 3) { IsUnique = true },
                new IndexAttribute("ix_2", 3) { IsUnique = true },
            }));
    
    modelBuilder.Entity<MyEntity2>()
        .Property(e => e.Column5)
        .HasColumnAnnotation(
            IndexAnnotation.AnnotationName,
            new IndexAnnotation(new[]
            {
                new IndexAttribute("ix_2", 4) { IsUnique = true },
                new IndexAttribute("ix_1", 4) { IsUnique = true },
            }));