实体框架代码第一设计

时间:2015-08-26 15:14:48

标签: c# entity-framework

我正在使用实体框架6为委员会目录进行设计,并且正在对如何构建我的实体进行一些指导。已经有一段时间了,因为我从头做过这样的事情,任何帮助都会非常感激。我更习惯先做这个数据库,而不是先编码,而且需要一段时间来改变我对事物的思考方式。

我有两个实体:

public class Councillor
{
    [Key]
    public int CouncillorId { get; set; }
    public string Name { get; set; }
    public ICollection<Committee> Committees { get; set; }
}

public class Committee
{
    [Key]
    public int CommitteeId { get; set; }
    public string CommitteeName { get; set; }
    public ICollection<Councillor> Councillors { get; set; }
}

多对多关系,因为议员可以参加许多委员会,委员会有许多委员。

除此之外,委员会还有担任主席,副主席或正式成员的议员。列表处理后者,但我正在寻找关于设置其他位置的建议(最佳实践)。

我目前正考虑过:

public class Committee
{
    [Key]
    public int CommitteeId { get; set; }
    public string CommitteeName { get; set; }
    public int ChairmanId { get; set; } // link to councillor entity
    public int ViceChairId { get; set; } // link to councillor entity
    public ICollection<Councillor> Councillors { get; set; }
}

这种方法的任何建议或问题?在数据库中,我可能只有一个委员会成员的链接表,其中包括CouncillorId,CommitteeId和Position。但是我无法理解如何将其转换为代码。

1 个答案:

答案 0 :(得分:1)

听起来你只需要添加一个联结表来加入你的议员和委员会。我将主席和副主席的财产保留在委员会的对象上。

public class Councillor
{
    [Key]
    public int CouncillorId { get; set; }

    public string Name { get; set; }

    // Junction Table Navigation Property
    [ForeignKey("CouncillorId")]
    public virtual ICollection<CouncillorCommittee> CouncilorCommittees { get; set; }
}

public class Committee
{
    [Key]
    public int CommitteeId { get; set; }

    public string Name { get; set; }

    public int ChairmanId { get; set; }

    public int ViceChairmanId { get; set; }

    [ForeignKey("ChairmanId")]
    public virtual Councillor Chairman { get; set; }

    [ForeignKey("ViceChairmanId")]
    public virtual Councillor ViceChairman { get; set; }

    // Junction Table Navigation Property
    [ForeignKey("CommitteeId")]
    public virtual ICollection<CouncillorCommittee> CouncilorCommittees { get; set; }
}

// Represents the joining table
public class CouncillorCommittee
{
    [Key]
    public int CouncillorCommitteeId { get; set; }

    public int CouncillorId { get; set; }

    public int CommitteeId { get; set; }

    [ForeignKey("CouncillorId")]
    public virtual Councillor Councillor { get; set; }

    [ForeignKey("CommitteeId")]
    public virtual Committee Committee { get; set; }
}

在你的代码中,你会得到这样的东西来获得委员会和议员的名单:

var committees =  from c
                  in Committees
                  select new
                  {
                      CommitteeName = c.Name,
                      Chairman = c.Chairman,
                      ViceChairman = c.ViceChairman,
                      Councillors = from cc
                                    in c.CouncilorCommittees
                                    select cc.Councillor
                  }

同样,要获得所有议员和他们所属的委员会,你可以这样做:

var councillors = from c
                  in Councillors
                  select new
                  {
                      CouncillorName = c.Name,
                      Committes = from cc
                                    in c.CouncilorCommittees
                                    select cc.Committee
                  }