本文介绍了非聚集主键实体框架代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Entity Framework Code First approach, can we define the Primary Key as non-clustered index and a combination of few other fields as clustered index.

谢谢

推荐答案

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

EntityTypeConfiguration does not provide a means of setting the Primary Key as a non-clustered index, but you can complete this by altering the initial migration used for table creation. There is an example here.

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

Here is an example of how to specify a clustered multiple-column index using attributes:

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

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

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

and an example of how to complete this using model builder:

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 }));

这篇关于非聚集主键实体框架代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 07:47