非群集主要密钥实体框架代码优先

Sat*_*jit 6 indexing entity-framework-6

在Entity Framework Code First方法中,我们可以将主键定义为非聚簇索引,并将其他几个字段组合为聚簇索引.

谢谢

Joh*_*ian 13

EF 6.2解决了这个问题.目前,它处于测试状态,但它确实有效.

首先,将EF升级到6.2:

Install-Package EntityFramework -Version 6.2.0-beta1 -Pre
Run Code Online (Sandbox Code Playgroud)

然后,在该OnModelCreating方法中,将IsClustered设置false为主键:

modelBuilder.Entity<Receipt>().HasKey(r => r.RecId, config => config.IsClustered(false) );
Run Code Online (Sandbox Code Playgroud)


Lar*_*rry 5

EntityTypeConfiguration 不提供将主键设置为非聚集索引的方法,但您可以通过更改用于表创建的初始迁移来完成此操作。有一个例子在这里

以下是如何使用属性指定聚集多列索引的示例:

[Index("IX_ColumnOneTwo", 1, IsClustered = true)]
public int ColumnOne { get; set;}

[Index("IX_ColumnOneTwo", 2, IsClustered = true)]
public int ColumnTwo { get; set; }
Run Code Online (Sandbox Code Playgroud)

以及如何使用模型构建器完成此操作的示例:

modelBuilder.Entity<ClassOne>() 
            .Property(t => t.ColumnOne) 
            .HasColumnAnnotation( 
                     "Index",  
                     new IndexAnnotation(new IndexAttribute("IX_ColumnOneTwo") { IsClustered = true }));
modelBuilder.Entity<ClassOne>() 
            .Property(t => t.ColumnTwo) 
            .HasColumnAnnotation( 
                     "Index",  
                     new IndexAnnotation(new IndexAttribute("IX_ColumnOneTwo") { IsClustered = true }));
Run Code Online (Sandbox Code Playgroud)


Rad*_*Zec 5

通过覆盖 DbContext 中的 OnModelCreating 有一个 Entity Framework Core Code First 的解决方案

  p.HasKey(b => b.ColumnId).ForSqlServerIsClustered(false);
Run Code Online (Sandbox Code Playgroud)

此代码将生成如下迁移:

 table.PrimaryKey("PK_Columns", x => x.ColumnId)
                        .Annotation("SqlServer:Clustered", false);
Run Code Online (Sandbox Code Playgroud)