本文介绍了Entity Framework中多个列的唯一键限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class Entity { [ Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string EntityId {get; set;} public int FirstColumn {get; set;} public int SecondColumn {get; set;} }
我想使 FirstColumn 和 SecondColumn 作为唯一的。
示例:
Id FirstColumn SecondColumn 1 1 1 = OK 2 2 1 = OK 3 3 3 = OK 5 3 1 =这个OK 4 3 3 = GRRRRR!这里错误有没有办法?
解决方案使用实体框架6.1,您现在可以执行此操作:
[Index(IX_FirstAndSecond,1,IsUnique = true)] public int FirstColumn {get;组; } [Index(IX_FirstAndSecond,2,IsUnique = true)] public int SecondColumn {get;组; }属性中的第二个参数是可以在索引中指定列的顺序。 更多信息: MSDN
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 ERRORIs there anyway to do that?
解决方案With Entity Framework 6.1, you can now do this:
[Index("IX_FirstAndSecond", 1, IsUnique = true)] public int FirstColumn { get; set; } [Index("IX_FirstAndSecond", 2, IsUnique = true)] public int SecondColumn { get; set; }The second parameter in the attribute is where you can specify the order of the columns in the index. More information: MSDN
更多推荐
Entity Framework中多个列的唯一键限制
发布评论