Code First EF模​​型问题

时间:2014-07-03 02:02:43

标签: c# entity-framework ef-code-first code-first

遇到问题Group.Members.Count()回到0。

public class Employee
{  
    public Guid Id { get; set; }
    public String Name { get; set; } 
    public Guid? PrimaryGroupId { get; set; }
    [ForeignKey("PrimaryGroupId")]
    public virtual Group PrimaryGroup { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
    public virtual ICollection<Activity> Activities { get; set; }
}


public class Group
{
    public Group()
    {
        Id = Guid.NewGuid();
    }
    public Guid Id { get; set; }
    public String Name { get; set; }
    public Guid? GroupOwnerId{ get; set; }
    [ForeignKey("GroupOwnerId")]
    public virtual Employee GroupOwner{ get; set; } 
    public virtual ICollection<Employee> Members{ get; set; } 
}

基本上,Employee将拥有一个主要组,并且只有一个主要组。这是可以为空的,因为并非所有员工都在一个团队中。一个集团可以拥有1名员工,该员工是集团所有者,也可以是将该集团称为“主要”集团的员工集合。

在视图中要求用户查看有多少员工将该组称为主要组。我正在使用此代码:Group.Members.Count()但结果得到0,尽管数据明显不同。也好像EF / Code First正在为这些关系生成辅助列,因为现在出现了FK列PrimaryGroup_Id

这是迁移为模型生成的内容....

CreateTable(
    "dbo.Employees",
    c => new
        {
            Id = c.Guid(nullable: false),
            Name = c.String(),
            IsActive = c.Boolean(nullable: false), 
            PrimaryGroupId= c.Guid(),
            PrimaryGroup_Id = c.Guid(),
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.Groups", t => t.PrimaryGroupId)
    .ForeignKey("dbo.Groups", t => t.PrimaryGroup_Id)
    .Index(t => t.PrimaryGroupId)
    .Index(t => t.PrimaryGroup_Id);

CreateTable(
    "dbo.Groups",
    c => new
        {
            Id = c.Guid(nullable: false),
            Name = c.String(),
            IsActive = c.Boolean(nullable: false), 
            GroupOwnerId= c.Guid(),
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.Employees", t => t.GroupOwnerId)
    .Index(t => t.GroupOwnerId);

疯狂的是,Employee表上存在两个PrimaryGroupId列,如果在编辑员工后将GUID从该记录复制到PrimaryGroup_Id列,则“组”视图上的计数会上升。感觉像某种FK问题。

1 个答案:

答案 0 :(得分:0)

EF必须在将MembersPrimaryGroupId关联起来时遇到问题,因此请通过明确声明反向导航属性来帮助它:[ForeignKey("PrimaryGroupId"), InverseProperty("Members")]

如果我复制您的代码(不包括AddressesPhoneNumbersActivities属性),我会获得额外的Id属性,但它被称为Group_Id

        CreateTable(
            "dbo.Employees",
            c => new
                {
                    Id = c.Guid(nullable: false),
                    Name = c.String(),
                    PrimaryGroupId = c.Guid(),
                    Group_Id = c.Guid(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey( "dbo.Groups", t => t.Group_Id)
            .ForeignKey( "dbo.Groups", t => t.PrimaryGroupId)
            .Index(t => t.PrimaryGroupId)
            .Index(t => t.Group_Id);

如果我如上所述添加InverseProperty("Members"),我会得到:

         CreateTable(
            "dbo.Employees",
            c => new
                {
                    Id = c.Guid(nullable: false),
                    Name = c.String(),
                    PrimaryGroupId = c.Guid(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Groups", t => t.PrimaryGroupId)
            .Index(t => t.PrimaryGroupId);