在实体框架6.1,我怎么可以使用IndexAttribute定义一个聚集索引? [英] In Entity Framework 6.1, how can I use the IndexAttribute to define a clustered index?

查看:207
本文介绍了在实体框架6.1,我怎么可以使用IndexAttribute定义一个聚集索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架6.1(代码优先)添加了通过 IndexAttribute 添加索引的可能性。该属性需要一个参数用于指定索引是否应该集群或非集群。

Entity Framework 6.1 (code-first) has added the possibility of adding indexes via the IndexAttribute. The attribute takes a parameter for specifying whether the index should be clustered or non-clustered.

与此同时,据我所知,实体框架要求每个实体都有一个主键(注释与 KeyAttribute ),并且该主键始终是作为一个的集群的关键。

At the same time, AFAIK, Entity Framework requires every entity to have a primary key (annotated with the KeyAttribute), and that primary key is always created as a clustered key.

所以,只要我申请了 IndexAttribute IsClustered = TRUE ,我得到一个错误,因为,由于关键时,已经的聚集索引。

Therefore, as soon as I apply the IndexAttribute with IsClustered = true, I get an error because, due to the key, there already is a clustered index.

所以,我怎么可以创建一个聚集索引不是使用主键在 IndexAttribute ?是的 IsClustered 属性 IndexAttribute 可用呢?

So, how can I create a clustered index that is not the primary key using the IndexAttribute? Is the IsClustered property of the IndexAttribute usable at all?

(对于多一点背景:我的映射只用于通过LINQ查询读一表我并不需要真正插入,更新或删除从该表的实体。所以,我并不需要一个主键在所有。理想情况下,我想一个表没有主键,但与阅读优化的非唯一,聚集索引。)

(For a little more context: I'm mapping a table that is only used for reading via LINQ queries. I do not need to actually insert, update, or delete entities from that table. Therefore, I don't need a primary key at all. Ideally, I'd like a table without a primary key, but with a non-unique, clustered index optimized for reading.)

修改(2014年4月11日):查看: //entityframework.codeplex.com/workitem/2212 。

推荐答案

有只能在一个聚集索引表并默认实体框架/ SQL Server会将它的主键。

There can only be one clustered index on a table and by default Entity Framework/Sql Server puts it on the primary key.

那么,使用的是 IsClustered 属性上不是主键索引?好问题! (+1)

So what use is the IsClustered attribute on an index that is not the primary key? Good question! (+1)

本类:

public class Blog
{
    [Key()]
    public int Id { get; set; }

    [MaxLength(256)]//Need to limit size of column for clustered indexes
    public string Title { get; set; }

    [Index("IdAndRating", IsClustered = true)]
    public int Rating { get; set; }

}



会产生这种迁移:

will generate this migration:

    public override void Up()
    {
        CreateTable(
            "dbo.Blogs",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Title = c.String(maxLength: 256),
                    Rating = c.Int(nullable: false),
                });
            .PrimaryKey(t => t.Id)
            .Index(t => t.Rating, clustered: true, name: "IdAndRating");
    }



ALTER迁移到这一点:

Alter the migration to this:

    public override void Up()
    {
        CreateTable(
            "dbo.Blogs",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Title = c.String(maxLength: 256),
                    Rating = c.Int(nullable: false),
                });

        CreateIndex("dbo.Blogs", 
                    new[] { "Rating", "Title" }, 
                    clustered: true, 
                    name: "IdAndRating");

    }

和应该没有主键,但与创建表在其他列的聚集索引

And that should create your table without a primary key but with the clustered index on the other columns

修改
在您的情况,你不需要插入,更新或删除数据,你并不需要一个完全成熟的实体,可以使用原始的SQL查询来填充类。您将需要你自己的SQL添加到迁移创建表,因为EF不会自动执行它,但是这意味着您可以创建就像你想要的表和索引。

EDIT In your scenario where you don't need to insert, update or delete data, you don't need a full blown entity, you could use raw sql queries to populate the classes. You would need to add your own sql to the migration to create the table because EF won't automate it, but that means you can create the table and index just as you want it.

这篇关于在实体框架6.1,我怎么可以使用IndexAttribute定义一个聚集索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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