Entity框架代码首先如何创建索引在后面的顺序? [英] How can Entity Framework Code First Create Index in Descendent order?

查看:258
本文介绍了Entity框架代码首先如何创建索引在后面的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var indexAttr = new IndexAttribute(IX_EmployeeNumber) 
{
IsClustered = true
};

属性(c => c.EmployeeNumber)
.HasColumnAnnotation(Index,new IndexAnnotation(indexAttr))
.HasMaxLength(8)
.IsRequired ();

添加迁移之后,我们将得到这个 CreateIndex 语句:

  CreateIndex(dbo.Employees EmployeeNumber,clustered:true,name:IX_EmployeeNumber); 

更新数据库之后,我们有一个这样的sql:

  CREATE CLUSTERED INDEX [IX_EmployeeNumber] ON [dbo]。[Employees] 

[EmployeeNumber] ASC
)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,SORT_IN_TEMPDB = OFF,DROP_EXISTING = OFF,ONLINE = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]
GO

我的问题是:如何以后代顺序创建此索引?如下:

  CREATE CLUSTERED INDEX [IX_EmployeeNumber] ON [dbo]。[Employees] 

[EmployeeNumber] DESC

我找不到相关参数 CreateIndex 是否可以在Code First迁移中实现?



当然,我们知道我们可以使用 DbMigration来处理所有内容。 Sql(...一些sql语句...)但是,如何使用 CreateIndex()

解决方案

看看如何自定义代码首次迁移从Rowan Miller博客(实体框架计划经理之一)。



这里描述如何覆盖 SqlServerMigrationsSqlGenerator sql generator以允许自定义参数影响生成的sql。



当它发生,场景ario他通过扩展了 CreateIndex 迁移操作的默认行为,以支持指定创建降序索引



但是,我不认为可以将此扩展点公开到您的代码优先域模型创建代码。


In fluent API, we can specify an Index on a field:

var indexAttr = new IndexAttribute("IX_EmployeeNumber")
{
    IsClustered = true
};

Property(c => c.EmployeeNumber)
    .HasColumnAnnotation("Index", new IndexAnnotation(indexAttr ))
    .HasMaxLength(8)
    .IsRequired();

After Add-Migration, we will get this CreateIndex statement:

CreateIndex("dbo.Employees", "EmployeeNumber", clustered: true, name: "IX_EmployeeNumber");

After a Update-Database, we have a sql like this:

CREATE CLUSTERED INDEX [IX_EmployeeNumber] ON [dbo].[Employees]
(
    [EmployeeNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

My question is: how can I create this index in descendent order? Something like:

CREATE CLUSTERED INDEX [IX_EmployeeNumber] ON [dbo].[Employees]
(
    [EmployeeNumber] DESC

I couldn't find related parameters in CreateIndex. Is it possible to be achieved in Code First migration?

Of course we know we could get everything working with DbMigration.Sql(...some sql statement...). But, how can we obtain the desired result with CreateIndex()?

解决方案

Take a look at how to customize code first migrations from the blog of Rowan Miller (one of the Entity Framework program managers).

This describes how to override the SqlServerMigrationsSqlGenerator sql generator to allow custom arguments to affect the generated sql.

As it happens, the scenario he walks through is extending the default behaviour of the CreateIndex migration operation to support specifying the creation of a descending index.

However, I don't think it is possible to expose this extension point to your code-first domain model creation code.

这篇关于Entity框架代码首先如何创建索引在后面的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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