0

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!

8
  • Possible duplicate of What is a NullReferenceException, and how do I fix it? Commented Dec 24, 2016 at 20:35
  • Please show C# code for Package and User entities that was generated by Entity Framework or written by yourself in case of code-first approach. Commented Dec 24, 2016 at 20:39
  • @jdphenix I have read the answers to that question but that is not the problem that I have, my problem is that Entity Framework id creating a reference for only one foreign key from the two mentioned above Commented Dec 24, 2016 at 20:40
  • @OleksandrKobylianskyi It'a a lot of generated code, in my question I have tried to simplify the problem Commented Dec 24, 2016 at 20:42
  • ok, then please check that Package class indeed contains User property Commented Dec 24, 2016 at 20:45

1 Answer 1

1

Try db.Packages.Include("User").Include("User1")

Sign up to request clarification or add additional context in comments.

2 Comments

It's working but I do not understand why, can you explain, please?
I guess you assume that Include loads all navigations properties with type User. But Include string parameter means concrete property not all properties of the type. You should call Include for each navigation property that you are going to include.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.