2014-02-25 57 views
7

這發生了沒有地方。我以前從未遇到過這個問題。我剛剛完成向我的SQL Azure數據庫添加一個表,該表將爲註冊我們的電子郵件列表的人員保存電子郵件。沒有任何關聯到該表,它只是單獨的。我回到VS並從我的數據庫更新我的模型,現在得到這些錯誤。問題與多重性和EF 6

Error 1 Error 113: Multiplicity conflicts with the referential constraint in Role 'Category' in relationship 'FK_Items_3'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. C:\Users\chuyita\Documents\Visual Studio 2012\Projects\gcaMusicExchange\gcaMusicExchange\myConnection.edmx 1043 11 gcaMusicExchange 

Error 2 Error 113: Multiplicity conflicts with the referential constraint in Role 'Manufacturer' in relationship 'FK_Items_4'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. C:\Users\chuyita\Documents\Visual Studio 2012\Projects\gcaMusicExchange\gcaMusicExchange\myConnection.edmx 1055 11 gcaMusicExchange 

我的理解是在談論同一個關係問題,但我並沒有改變任何東西,不明白爲什麼它現在抱怨。

我檢查了這一點http://msdn.microsoft.com/en-us/data/jj591620.aspx ,結束了從0..1 (Zero or One of Manufacturer)1 (One of Manufacturer)其最終解決的第一個錯誤改變了我的設計師2間的關係。我對第二個錯誤也做了同樣的處理,現在問題消失了。我並不完全確定這裏發生了什麼事情,並且擔心這樣繼續我的項目可能會導致更多的問題。

任何人都可以提供任何可能出現錯誤的見解嗎?

public partial class Category 
{ 
    public Category() 
    { 
     this.Items = new HashSet<Item>(); 
    } 

    public int id { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<Item> Items { get; set; } 
} 

public partial class Manufacturer 
{ 
    public Manufacturer() 
    { 
     this.Items = new HashSet<Item>(); 
    } 

    public int id { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<Item> Items { get; set; } 
} 

public partial class Item 
{ 
    public Item() 
    { 
     this.PrivateMessages = new HashSet<PrivateMessage>(); 
    } 

    public int id { get; set; } 
    public int SellingUserId { get; set; } 
    public string Title { get; set; } 
    public string Body { get; set; } 
    public Nullable<decimal> Price { get; set; } 
    public Nullable<bool> IsActive { get; set; } 
    public Nullable<System.DateTime> PostDate { get; set; } 
    public Nullable<System.DateTime> LastGarbageCollectionDate { get; set; } 
    public Nullable<int> SoldToUserId { get; set; } 
    public string Uri1 { get; set; } 
    public string Uri2 { get; set; } 
    public Nullable<int> ZipCode { get; set; } 
    public int CategoryId { get; set; } 
    public int ManufacturerId { get; set; } 
    public Nullable<bool> WaitingForSoldConfirmation { get; set; } 
    public Nullable<int> PendingSoldTo { get; set; } 
    public Nullable<int> Views { get; set; } 
    public Nullable<bool> AcceptsTrades { get; set; } 
    public Nullable<int> MakeId { get; set; } 
    public Nullable<int> ModelId { get; set; } 
    public Nullable<int> YearId { get; set; } 
    public Nullable<int> Type { get; set; } 
    public string Uri3 { get; set; } 
    public string Uri4 { get; set; } 
    public string Uri5 { get; set; } 

    public virtual Category Category { get; set; } 
    public virtual Manufacturer Manufacturer { get; set; } 
    public virtual VehicleMake VehicleMake { get; set; } 
    public virtual VehicleModel VehicleModel { get; set; } 
    public virtual VehicleYear VehicleYear { get; set; } 
    public virtual ICollection<PrivateMessage> PrivateMessages { get; set; } 
} 

回答

11

如果你想擁有不可空的鍵,你需要一個1->多的關係。將相關關係更改爲0 - >許多。

我假設關係約束在某個時候發生了變化,這是數據第一個項目嗎?如果是這樣,你確定約束在數據庫級沒有改變?

+0

是的,這是正確的。我不確定有人將FK值設爲NonNullable。 – Adrian

+0

感謝它爲我工作。在vb.net項目中,我將「.WithOptional」改爲「.WithRequired」來解決問題。 – Faisal

+0

我正在處理模型的第一個項目,並且我必須有0..1 - *類型的關聯,因爲* end可能沒有分配任何對象。儘管0..1的結尾可能有0,1或許多分配。所以我不能有1 - *型關係。我因爲這個BS錯誤而被卡住了。 –