Creating string index with Code first

后端 未结 2 1695
深忆病人
深忆病人 2020-12-08 13:38

I\'m using Entity Framework 6.1 code-first and my domain model is below.

class Item
{
    [Index]
    public string CreatedBy { set; get; }
} 
2条回答
  •  死守一世寂寞
    2020-12-08 13:53

    If you use EntityTypeConfiguration aka mappings :

    public class MyPocoEntitiyTypeConfig : EntityTypeConfiguration where T:class
    {
    
    }
    
    public class MyPocoEnt
    {
        public virtual string MyProp { get; set; }
    }
    
    public class MyPocoEntMapping : MyPocoEntitiyTypeConfig
    {
        public MyPocoEntMapping()
        {
                Property(x => x.MyProp).HasMaxLength(300);
                Property(x => x.MyProp).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("MyProp") { IsUnique = true }));
            }
        }
    }
    

提交回复
热议问题