unique-key

Unique Key constraints for multiple columns in Entity Framework

怎甘沉沦 提交于 2019-11-26 01:12:50
问题 I\'m using Entity Framework 5.0 Code First; public class Entity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string EntityId { get; set;} public int FirstColumn { get; set;} public int SecondColumn { get; set;} } I want to make the combination between FirstColumn and SecondColumn as unique. Example: Id FirstColumn SecondColumn 1 1 1 = OK 2 2 1 = OK 3 3 3 = OK 5 3 1 = THIS OK 4 3 3 = GRRRRR! HERE ERROR Is there anyway to do that? 回答1: With Entity Framework 6.1, you can

Unique key with EF code first

…衆ロ難τιáo~ 提交于 2019-11-25 23:49:24
问题 I have a following model in my project public class Category { public Guid ID { get; set; } [Required(ErrorMessage = \"Title cannot be empty\")] public string Title { get; set; } } and I\'m trying to make Title as unique key, I googled for the solution, but couldn\'t find any. Can any suggest me how to do it, please? 回答1: Unfortunately you can't define it as unique key in code first because EF doesn't support unique keys at all (it is hopefully planned for next major release). What you can do