2016-12-24 85 views
0

我有兩個表實體框架空指針引用異常

之間的關係

第一個表:用戶

Id | Name | 

二表:包裝

Id | UserSenderId | UserReceiverId 

我使用實體框架與.Net Framework 3.5和我的問題是,實體框架正在創建兩個參考電話號碼爲UserReference1UserReference,但UserReference1始終爲空。如果我不包括User表是這樣的:

db.Packages.Include("User") 

然後UserReference也爲空。

我真的用盡了這個問題的想法,有沒有使用多個外鍵指向相同的主鍵的問題?

EDIT 1:

Package部分類的用戶參考:

[global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("Tema4Model", "FK_Package_User_Receiver", "User")] 
[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
[global::System.Xml.Serialization.XmlIgnoreAttribute()] 
[global::System.Xml.Serialization.SoapIgnoreAttribute()] 
[global::System.Runtime.Serialization.DataMemberAttribute()] 
public User User 
{ 
    get 
    { 
     return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<User>("Tema4Model.FK_Package_User_Receiver", "User").Value; 
    } 
    set 
    { 
     ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<User>("Tema4Model.FK_Package_User_Receiver", "User").Value = value; 
    } 
} 

/// <summary> 
/// There are no comments for User in the schema. 
/// </summary> 
[global::System.ComponentModel.BrowsableAttribute(false)] 
[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
[global::System.Runtime.Serialization.DataMemberAttribute()] 
public global::System.Data.Objects.DataClasses.EntityReference<User> UserReference 
{ 
    get 
    { 
     return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<User>("Tema4Model.FK_Package_User_Receiver", "User"); 
    } 
    set 
    { 
     if ((value != null)) 
     { 
      ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedReference<User>("Tema4Model.FK_Package_User_Receiver", "User", value); 
     } 
    } 
} 

/// <summary> 
/// There are no comments for User1 in the schema. 
/// </summary> 
[global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("Tema4Model", "FK_Package_User_Sender", "User")] 
[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
[global::System.Xml.Serialization.XmlIgnoreAttribute()] 
[global::System.Xml.Serialization.SoapIgnoreAttribute()] 
[global::System.Runtime.Serialization.DataMemberAttribute()] 
public User User1 
{ 
    get 
    { 
     return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<User>("Tema4Model.FK_Package_User_Sender", "User").Value; 
    } 
    set 
    { 
     ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<User>("Tema4Model.FK_Package_User_Sender", "User").Value = value; 
    } 
} 

/// <summary> 
/// There are no comments for User1 in the schema. 
/// </summary> 
[global::System.ComponentModel.BrowsableAttribute(false)] 
[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")] 
[global::System.Runtime.Serialization.DataMemberAttribute()] 
public global::System.Data.Objects.DataClasses.EntityReference<User> User1Reference 
{ 
    get 
    { 
     return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<User>("Tema4Model.FK_Package_User_Sender", "User"); 
    } 
    set 
    { 
     if ((value != null)) 
     { 
      ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedReference<User>("Tema4Model.FK_Package_User_Sender", "User", value); 
     } 
    } 
} 

任何幫助理解,謝謝!

+0

的可能的複製[什麼是一個NullReferenceException,以及如何解決?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and- – jdphenix

+0

請顯示實體框架生成的或由您自己編寫的代碼優先方法中的「Package」和「User」實體的C#代碼。 –

+0

@jdphenix我已經閱讀了這個問題的答案,但這不是我有的問題,我的問題是實體框架ID創建一個從上面提到的兩個外鍵只有一個參考 – sixfeet

回答

1

嘗試 db.Packages.Include("User").Include("User1")

+0

它的工作,但我不明白爲什麼,你能解釋一下嗎? – sixfeet

+0

我想你認爲'Include'加載類型爲'User'的所有導航屬性。但'Include'字符串參數意味着具體屬性不是該類型的所有屬性。你應該爲你將要包含的每個導航屬性調用'Include'。 –

+0

噢,我明白了,謝謝! – sixfeet