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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sixfeet
  • 934
  • 4
  • 16
  • 33
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jdphenix Dec 24 '16 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. – Oleksandr Kobylianskyi Dec 24 '16 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 – sixfeet Dec 24 '16 at 20:40
  • @OleksandrKobylianskyi It'a a lot of generated code, in my question I have tried to simplify the problem – sixfeet Dec 24 '16 at 20:42
  • ok, then please check that `Package` class indeed contains `User` property – Oleksandr Kobylianskyi Dec 24 '16 at 20:45
  • It's very odd how the classes are generated in .Net 3.5, I have worked with Entity Framework in .Net 4.5 but is nothing like that – sixfeet Dec 24 '16 at 20:48
  • So you get either `User` or `User1` to be null or both? – Oleksandr Kobylianskyi Dec 24 '16 at 21:04
  • If do not include user like in the question both are null. If I include User then only User1 is null – sixfeet Dec 24 '16 at 21:06

1 Answers1

1

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

  • It's working but I do not understand why, can you explain, please? – sixfeet Dec 24 '16 at 21:29
  • 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. – Oleksandr Kobylianskyi Dec 24 '16 at 21:35