在主键实体框架 6.0 上创建非聚集索引 [英] create non clustered index on primary key entity framework 6.0
本文介绍了在主键实体框架 6.0 上创建非聚集索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道 这个,它指出不可能先通过代码创建具有非聚集索引的主键.还是这样吗?
I am aware of this, which states that it is not possible to create a primary key with non clustered index via code first. Is this still the case?
理想情况下,我想通过 EntityTypeConfiguration 指定我的主键 (Guid) 有一个非聚集索引,而另一列 (int) 有一个聚集索引.
Ideally, I would like to specify via EntityTypeConfiguration, that my primary key (Guid) has a non-clustered index and there is another column (int) with a clustered index.
推荐答案
AFAIK 这在 EntityTypeConfiguration 中是不可能的.但是,您可以使用 Code-First 迁移来做到这一点.工作示例:
AFAIK this is not possible with EntityTypeConfiguration. However you can do this with Code-First migrations. Working example:
public class Product
{
public Guid Id
{ get; set; }
public int Price
{ get; set; }
}
class AppDbContext : DbContext
{
public DbSet<Product> Products
{ get; set; }
}
public partial class InitialCreate : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Products",
c => new
{
Id = c.Guid(nullable: false),
Price = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id, clustered: false)
.Index(t => t.Price, clustered: true);
}
public override void Down()
{
DropIndex("dbo.Products", new[] { "Price" });
DropTable("dbo.Products");
}
}
结果:
CREATE TABLE [dbo].[Products] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[Price] INT NOT NULL,
CONSTRAINT [PK_dbo.Products] PRIMARY KEY NONCLUSTERED ([Id] ASC)
);
GO
CREATE CLUSTERED INDEX [IX_Price]
ON [dbo].[Products]([Price] ASC);
这篇关于在主键实体框架 6.0 上创建非聚集索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文