如何停止EF4.1 Code-First,为实体PK创建Culstered索引 [英] How to stop EF4.1 Code-First to create Culstered index for the entity PK

查看:296
本文介绍了如何停止EF4.1 Code-First,为实体PK创建Culstered索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下简单实体类,初始化数据库时,EF4.1 Code-First将为PK UserId 列创建聚簇索引。

With the following simple entity class, EF4.1 Code-First will create Clustered Index for the PK UserId column when intializing the database.

    public class User
    {
        [Key]
        public int UserId { get; set; }
        public int AppId  { get; set; }
        public string UserName { get; set; }
    }

为了表现,我的设计目标是保持生成的用户表物理根据 AppId coulmn聚集到PK。

For performance sake, my design goals is to keep the generated Users table physical Clustered according to the AppId coulmn not to the PK.

在我的Initializer类上,我试图手动删除自动生成的PK聚集索引并创建我需要的任何聚集索引,但是没有线索来预测自动生成的名称 PK__Users__25518C17 索引!

On my Initializer class I've tried to manually drop the autogenerated PK clustered index and create whatever clustered index I need, but no clue here to predict the autogenerated name PK__Users__25518C17 for the index!

我是新一代的Code-First世界,真的不知道我的设计目标是否有任何解决方法。

I'm new to Code-First world, and really don't know if there's any workaround for my design goals.

提前感谢

推荐答案

您不需要预测自动生成的索引的名称。您只需要选择索引的名称。对于SQL服务器,您可以使用以下查询:

You don't need to predict name of auto generated index. You just need to select name of the index. For SQL server you can use query like:

SELECT I.Name
FROM sys.indexes AS I
INNER JOIN sys.tables AS T ON
    I.object_Id = T.object_Id
WHERE I.is_primary_key = 1 
    AND T.Name = 'Users'

一旦您在自定义初始化程序中获取名称,您可以更改旧的索引并创建一个新的索引。

Once you get the name in your custom initializer you can alter old index and create a new one.

这篇关于如何停止EF4.1 Code-First,为实体PK创建Culstered索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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