Entity Framework中的多个列的唯一键约束 [英] Unique Key constraints for multiple columns in Entity Framework

查看:2230
本文介绍了Entity Framework中的多个列的唯一键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Entity Framework 5.0 Code First;

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

我想使

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

有没有反正?

推荐答案

使用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

这篇关于Entity Framework中的多个列的唯一键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆