Does Entity Framework 5 support unique constraints?

后端 未结 5 552
日久生厌
日久生厌 2020-12-10 01:49

Wondering if Entity Framework 5 supports unique constraints on entity properties? If so, how can I specify that a property should be unique?

5条回答
  •  借酒劲吻你
    2020-12-10 02:32

    As mentioned above it is not supported but as for now when I create a database programmatically (Code First) I use the following code in the init db section:

    MainDBContext mainDBContext = new MainDBContext();
    
    mainDBContext.Database.ExecuteSqlCommand("ALTER TABLE table_name ADD CONSTRAINT uc_FieldName UNIQUE(FieldName)");
    
    mainDBContext.Dispose();
    

提交回复
热议问题