在主键实体框架6.0上创建非聚簇索引 [英] create non clustered index on primary key entity framework 6.0

查看:234
本文介绍了在主键实体框架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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆