2014-10-17 31 views
4

我有一個類的電子郵件,看起來像:獨特的多列codefirst

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

爲確保唯一我在SubjectFromSentOn

指合成關鍵這產生的問題是,當主題超過128個字符,驗證失敗。所以我只在其上放置了一個[MaxLength]屬性。但現在它不能成爲關鍵列

我該怎麼辦?有沒有一種方法可以確保唯一性而不是關鍵?

+1

我可以給兩個不同的電子郵件具有相同主題。如果'SentOn'具有毫秒精度,那麼'From'和'SentOn'應該足夠了。 – 2014-10-17 22:04:49

+0

從不應該NVarChar(MAX) – 2016-03-04 18:07:10

回答

0

看看這個SO張貼在這裏,你可以添加一個索引以確保唯一性,無論是作爲一個屬性或使用EF FluentAPI Setting unique Constraint with fluent API?

注意你必須要使用EF6.1或更高版本。這裏是MSDN article

編輯:

檢查各地here和MSDN位後,它看起來像有對PK的限制和索引鍵是900個字節或更少,所以你會想用你身份作爲關鍵,並以另一種方式確保獨特性。

舉一個例子,我試圖手動創建的主題柱,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. which really means almost all real-world entries will fail.

因此,儘管你可以手動繞過128長度的限制,但不推薦你很可能會收到錯誤和丟失的數據。而EF只會讓你擁有128的關鍵長度 - 不知道如果你落後它並改變它,它會做什麼。

+0

它仍然抱怨不能成爲關鍵列:( – WindowsMaker 2014-10-17 20:32:45

+1

一個選項,以確保唯一性將獲得你想保持獨特的屬性的哈希,並使用它作爲一個獨特的列 - 或(不太高性能的)在存儲之前檢查集合 – tophallen 2014-10-17 21:48:34

3

如果您正在使用EF 6.1,您可以使用Multiple-Column Indexes功能:

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

在我的情況中也做過這樣的事情,但是當我生成初始配置(Add-Migration)時,遷移的Up()方法僅爲第一列:.Index(t => t.XXXXX,unique = true,name:「ÏX_XXXX」) – 2015-06-10 23:00:57

+1

當我在一個字符串的屬性/列上進行此操作時,EF會給出以下錯誤:表中的'Email'列'dbo.Patients'的類型對索引中的鍵列無效。 – TravisO 2017-06-01 12:36:33