实体框架过滤器索引 [英] Entity Framework Filter Index

查看:89
本文介绍了实体框架过滤器索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用EF 6.1.x代码.

I use EF 6.1.x Code First.

我已阅读到EF最新版不支持带有过滤器表达式的索引.

I have read that an Index with Filter Expression is not supported by EF latest.

SO也没有解决方案:

There is also no solution on SO:

EF 6.1唯一可空索引

一年后,使Filter Index与Code First和DbMigrations一起工作的工作方式是什么?

One year later, what is the working way to make a Filter Index work with Code First and DbMigrations?

CREATE UNIQUE NONCLUSTERED INDEX [IX_DefaultLanguageApplicationId] ON [dbo].[Languages]
(
    [IsDefaultLanguage] ASC,
    [ApplicationId] ASC,
)
WHERE ([IsDefaultLanguage]=(1))

推荐答案

在EF 6.1中,使其与Code First和DbMigrations一起使用的工作方法是在DbMigration类中使用Sql方法:

In EF 6.1, the working way to make the this work with Code First and DbMigrations is to use the Sql method in the DbMigration class:

public partial class AddIndexes : DbMigration
{
    public override void Up()
    {
        Sql(@"CREATE UNIQUE NONCLUSTERED INDEX
             [IX_DefaultLanguageApplicationId] ON [dbo].[Languages]
             (
                [IsDefaultLanguage] ASC,
                [ApplicationId] ASC 
             )
             WHERE ([IsDefaultLanguage]=(1))");

    }

    public override void Down()
    {
        DropIndex("dbo.Languages", "IX_DefaultLanguageApplicationId");
    }
}

但是我意识到您可能正在问您是否可以使用6.1中引入的IndexAttribute创建索引,但带有过滤器-答案是否"

But I realise that you are probably asking if you can create an index using the IndexAttribute introduced in 6.1, but with an Filter - the answer to that is "No"

几乎重复以下内容:实体框架6.1-使用INCLUDE语句创建索引

这篇关于实体框架过滤器索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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