简体   繁体   中英

Entity Framework null pointer reference exception

I have a relationship between two tables

First table: User

Id | Name | 

Second table: Package

Id | UserSenderId | UserReceiverId

I use Entity Framework with .Net Framework 3.5 and the problem I have is that Entity Framework is creating two references UserReference1 and UserReference but UserReference1 is always null. If I do not include the User table like this:

db.Packages.Include("User")

then UserReference is also null.

I really run out of ideas with this problem, is there an issue using multiple foreign keys pointing to the same primary key?

EDIT 1:

The user reference in Package partial class:

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

Any help is appreciated, thanks!

尝试db.Packages.Include("User").Include("User1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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