实体框架中多列的唯一键约束

编程入门 行业动态 更新时间:2024-10-09 08:28:15
本文介绍了实体框架中多列的唯一键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是实体框架 5.0 代码优先;

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;} }

我想让 FirstColumn 和 SecondColumn 之间的组合是唯一的.

I want to make the combination between FirstColumn and SecondColumn as unique.

示例:

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?

推荐答案

使用 Entity Framework 6.1,您现在可以执行以下操作:

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; }

属性中的第二个参数用于指定索引中列的顺序.更多信息:MSDN

The second parameter in the attribute is where you can specify the order of the columns in the index. More information: MSDN

更多推荐

实体框架中多列的唯一键约束

本文发布于:2023-10-19 01:57:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1506052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:一键   实体   框架   中多列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!