如何确保两个模型字段在一起是唯一的?

时间:2014-05-05 22:22:24

标签: c# asp.net-mvc-5 unique-constraint

我有以下型号:

public class tbMailingList
{
    public int ID { get; set; }

    [Display(Name = "Group")]
    public int tbGroupID { get; set; }

    [Display(Name = "Recipient")]
    public int tbRecipientID { get; set; }

    public virtual tbGroup tbGroup { get; set; }
    public virtual tbRecipient tbRecipient { get; set; }
}

如何确保tbGroupID和tbRecipientID都是使用C#和MVC5的唯一对?

给定对(tbGroupID,tbRecipient)必须是唯一的

  • 第1行(1,1)
  • 第2行(1,2)
  • 第3行(2,1)
  • 第4行(1,2)不允许重复'第2行'

我做了很多搜索,但没有答案。

1 个答案:

答案 0 :(得分:0)

如果使用EF

,可以尝试指定db中的唯一键
[Key, Column(Order = 0)]
 public int ID { get; set; }
 [Key, Column(Order =1)]
 [Display(Name = "Group")]
 public int tbGroupID { get; set; }
相关问题