Entity Framework Filter Index

ef-code-first entity-framework entity-framework-6

Question

I use EF 6.1.x Code First.

I've read that the most recent version of EF does not support an index with a filter expression.

Additionally, SO: offers no resolution.

Unique Nullable Index: EF 6.1

What is the current working method for getting a Filter Index to work with Code First and DbMigrations after a year?

CREATE UNIQUE NONCLUSTERED INDEX [IX_DefaultLanguageApplicationId] ON [dbo].[Languages]
(
    [IsDefaultLanguage] ASC,
    [ApplicationId] ASC,
)
WHERE ([IsDefaultLanguage]=(1))
1
25
11/13/2017 10:28:34 PM

Accepted Answer

The best method for making this work with Code First and DbMigrations in EF 6.1 is to use theSql approach in theDbMigration 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");
    }
}

However, I am aware that you are likely asking if you can utilizing the IndexAttribute introduced in 6.1, create an index. with a filter. To that, the answer is "No."

nearly identical to: Entity Framework 6.1: Use the INCLUDE command to create an index

29
5/23/2017 12:17:54 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow