How to create index in Entity Framework 6.2 with code first

前端 未结 10 466
不知归路
不知归路 2020-11-29 19:16

Is there a way to create an index on a property/column using code-first, instead of using the new IndexAttribute ?

10条回答
  •  抹茶落季
    2020-11-29 19:55

    From EF 6.1 onward the attribute [Index] is supported.
    Use [Index(IsUnique = true)] for unique index.
    Here is the link from Microsoft

    public class User 
    { 
        public int UserId { get; set; } 
    
        [Index(IsUnique = true)] 
        [StringLength(200)] 
        public string Username { get; set; } 
    
        public string DisplayName { get; set; } 
    }
    

提交回复
热议问题