Entity Framework Code First Fluent Api: Adding Indexes to columns

前端 未结 15 1365
一生所求
一生所求 2020-11-30 17:43

I\'m running EF 4.2 CF and want to create indexes on certain columns in my POCO objects.

As an example lets say we have this employee class:

public c         


        
15条回答
  •  孤独总比滥情好
    2020-11-30 18:12

    Extending Tsuushin's answer above to support multiple columns and unique constraints:

        private void CreateIndex(RBPContext context, string field, string table, bool unique = false)
        {
            context.Database.ExecuteSqlCommand(String.Format("CREATE {0}NONCLUSTERED INDEX IX_{1}_{2} ON {1} ({3})", 
                unique ? "UNIQUE " : "",
                table,
                field.Replace(",","_"),
                field));
        } 
    

提交回复
热议问题