实体框架空指针引用异常

时间:2016-12-24 20:33:04

标签: c# sql entity-framework

我有两个表之间的关系

第一个表格:用户

Id | Name | 

第二个表:

Id | UserSenderId | UserReceiverId

我在.Net Framework 3.5中使用Entity Framework,我遇到的问题是Entity Framework正在创建两个引用UserReference1UserReference,但UserReference1始终为null。如果我不包含这样的User表:

db.Packages.Include("User")

然后UserReference也为空。

我真的用完了这个问题的想法,使用指向同一主键的多个外键是否存在问题?

编辑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);
        }
    }
}

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

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