Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.3 Code Migrations with CreateIndex and Anonymous Arguments

I am trying to create an index using EF Code Migrations. The index looks something like:

CREATE INDEX [IX_RatingId_CreatedAt] ON [Users] 
(
[RatingId] ASC,
[CreatedAt] ASC
)
INCLUDE (Id, Email, DomainId)

The code I have so far is:

CreateIndex("Users",
             new string[] { "RatingId", "CreatedAt" },
             false,
             "IX_RatingId_CreatedAt"
           );

This will create the index for me but it will not include the columns. The CreateIndex method has an override that takes something called anonymousArguments. I can't really find that much information about it so I tried something like:

CreateIndex("Users",
             new string[] { "RatingId", "CreatedAt" },
             false,
             "IX_RatingId_CreatedAt",
             new { INCLUDE = "(Id, Email, DomainId)" });

There was no exception but it didn't work.

Is it possible to create the above index using the CreateIndex method or do I have to use the Sql method to write out the T-SQL in my migration? How do one use anonymous arguments correctly?

like image 628
Thomas Avatar asked May 07 '12 21:05

Thomas


1 Answers

The anonymous arguments are meant to be provider specific. As far as I've been able to find out, they are not used by the MSSQL provider (please correct me if I'm wrong).

There is no built in overload for CreateIndex that handles included columns. Either you'll have to fall back to manual SQL, or you can create the index over all columns.

CreateIndex("Users",
         new string[] { "RatingId", "CreatedAt", "Id", "Email", "DomainId" },
         false,
         "IX_RatingId_CreatedAt"
       );
like image 110
Anders Abel Avatar answered Oct 30 '22 21:10

Anders Abel