EF CodeFirst创建非集群主键索引 [英] EF CodeFirst create non-clustered primary key index

查看:279
本文介绍了EF CodeFirst创建非集群主键索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF 4.1 CodeFirst创建我的数据库.似乎EF正在使用聚集索引创建所有主键,这在一种情况下(可能是更多情况)对我们而言不是最佳的. 有没有办法告诉EF使用主键作为非聚集索引来生成此表?

当然,我们可以在创建数据库后使用自定义脚本手动完成此操作,但是我们有很多指向此表的外键,如果有更好,更直接的方法,那将是非最佳的解决方案数据库创建过程中已经完成的相同方法. 任何帮助表示赞赏

解决方案

没有方法可以控制SQL代码首先生成-它是代码优先,并且其当前的哲学(实际上是不好的)是:您对SQL知识一无所知数据库,您不必理会.

更改此行为的唯一方法是编写自定义数据库初始化程序,使用自定义数据库查询来搜索创建的索引,删除该索引并创建一个新索引.一些参考:

初始化程序将使您的解决方案依赖于具体的数据库服务器产品.

I'm using EF 4.1 CodeFirst to create my DB. It seems that EF is creating all primary keys with clustered index, which is not optimal for us in one case(possibly more cases). Is there a way to tell EF to generate this table with primary key as a non-clustered index ?

Of course we could do it manually using custom script after database is already created, but we have a lot of foreign keys pointing to this table, it would be quite a non-optimal solution if there is a better, more straight forward way to do the same already during DB creation. Any help appreciated

解决方案

No there is no way to control SQL code first generates - it is code first and its current philosophy (the bad one actually) is: you know nothing about the database and you don't bother.

The only way to change this behavior is to write custom database initializer, use custom database queries to search for created index, remove that index and create a new one. Some references:

The initilizer will make your solution dependent on the concrete database server product.

这篇关于EF CodeFirst创建非集群主键索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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