Entity Framework Code First Fluent Api: Adding Indexes to columns

前端 未结 15 1391
一生所求
一生所求 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:13

    To build on frozen's response, you can hand code it into a migration yourself.

    First, go to the Package Manager Console and create a new migration with add-migration, then give it a name. A blank migration will appear. Stick this in:

        public override void Up()
        {
            CreateIndex("TableName", "ColumnName");
        }
    
        public override void Down()
        {
            DropIndex("TableName",new[] {"ColumnName"});
        }
    

    Note that if you're using a string field it needs to be capped to a length of 450 chars as well.

提交回复
热议问题