1

我有这样的设置:

[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; } // ???
}

用户是否可以知道它属于哪个父分支?上面的代码不会填充 ParentBranch。

实体框架版本 5.0 .NET 4.0 c#

4

1 回答 1

2

尝试使导航属性虚拟化,

[Table("tablename...")]
public class Branch
{
    public Branch()
    {
        Users = new List<User>();
    }
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual 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 virtual Branch ParentBranch { get; set; } // ???
}
于 2012-10-31T05:01:54.797 回答