Entity Framework - Store parent reference on child relationship (one -> many)

Posted by contactmatt on Stack Overflow See other posts from Stack Overflow or by contactmatt
Published on 2012-10-31T04:57:38Z Indexed on 2012/10/31 5:00 UTC
Read the original article Hit count: 160

I have a setup like this:

[Table("tablename...")]
public class Branch
{
    public Branch()
    {
        Users = new List<User>();
    }
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public List<User> Users { get; set; }
}

[Table("tablename...")]
public class User
{
    [Key]
    public int Id {get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    [ForeignKey("ParentBranch")]
    public int? ParentBranchId { get; set; } // Is this possible?
    public Branch ParentBranch { get; set; } // ???
}

Is it possible for the User to know what parent branch it belongs to? The code above is not working.

Entity Framework version 5.0 .NET 4.0 c#

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework