简体   繁体   English

EF6 codefirst中的唯一多列

[英]Unique multiple column in EF6 codefirst

I have a class Email that looks like : 我有一个类似于以下内容的电子邮件:

public class Email
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public string From { get; set; }
    public DateTime SentOn { get; set; }
    public List<string> To { get; set; }
}

To ensure uniqueness I made a compound key on Subject , From and SentOn 为了确保唯一性,我在SubjectFromSentOn上创建了一个复合键

This created the problem that when Subject excess 128 characters, validation fails. 这造成了当Subject超过128个字符时,验证失败的问题。 So I just put a [MaxLength] attribute on it. 所以我只是在它上面放了一个[MaxLength]属性。 But now it can't be a key column 但现在它不能成为关键专栏

What should I do? 我该怎么办? Is there a way to ensure uniqueness without being a key? 有没有办法确保独特性而不是关键?

If You are using EF 6.1 , you can use Multiple-Column Indexes feature: 如果您使用的是EF 6.1 ,则可以使用多列索引功能:

public class Email
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int Id { get; set; }
   [Index("IX_EmailUniqueness", 1, IsUnique = true)]
   public string Subject { get; set; }
   public string Body { get; set; }
   [Index("IX_EmailUniqueness", 2, IsUnique = true)]
   public string From { get; set; }
   [Index("IX_EmailUniqueness", 3, IsUnique = true)]
   public DateTime SentOn { get; set; }
   public List<string> To { get; set; }
}

Check out this SO post here, you can add an index to ensure uniqueness, as either an attribute or using EF FluentAPI Setting unique Constraint with fluent API? 在这里查看此SO帖子,您可以添加索引以确保唯一性,作为属性或使用EF FluentAPI 使用流畅的API设置唯一约束?

Note you have to be using EF6.1 or later. 请注意,您必须使用EF6.1或更高版本。 Here is the MSDN article 这是MSDN文章

EDIT: 编辑:

After checking around here and msdn a bit, and it looks like there is a limit on PK's and index keys being 900 bytes or less so you will want to use your identity as a key, and ensure uniqueness in another way. 这里和msdn稍微检查之后,看起来PK和索引键的限制是900字节或更少,因此您将希望使用您的身份作为密钥,并以另一种方式确保唯一性。

For an example, I tried manually creating the subject column as unique with a length of 4000 and I got this error: 举个例子,我尝试手动创建主题列为唯一,长度为4000,我收到此错误:

Warning! The maximum key length is 900 bytes. The index 'UQ__Emails__A2B1D9048E9A2A16' has maximum length of 8000 bytes. For some combination of large values, the insert/update operation will fail. and if you were to do the clustered key option, you would get this warning on creation (and I made the length 4000 on each column) Warning! The maximum key length is 900 bytes. The index 'PK_dbo.Emails' has maximum length of 16012 bytes. For some combination of large values, the insert/update operation will fail. 如果你要做集群密钥选项,你会在创建时得到这个警告(我在每列上做了长度4000) Warning! The maximum key length is 900 bytes. The index 'PK_dbo.Emails' has maximum length of 16012 bytes. For some combination of large values, the insert/update operation will fail. Warning! The maximum key length is 900 bytes. The index 'PK_dbo.Emails' has maximum length of 16012 bytes. For some combination of large values, the insert/update operation will fail. which really means almost all real-world entries will fail. 这真的意味着几乎所有真实世界的参赛作品都会失败。

So while you can manually get around the 128 length limit, it's not recommended and you will most likely get errors and lost data. 因此,虽然您可以手动绕过128长度限制,但不建议这样做,您很可能会收到错误并丢失数据。 and EF will only let you have a key length of 128 - not sure what it would do if you go behind it and alter that. 和EF只会让你的密钥长度达到128 - 不知道如果你支持它并改变它会怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM