Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架代码优先设计_C#_Entity Framework - Fatal编程技术网

C# 实体框架代码优先设计

C# 实体框架代码优先设计,c#,entity-framework,C#,Entity Framework,我正在使用EntityFramework6设计一个议员目录,我正在寻找一些关于如何构建我的实体的指导。我已经有一段时间没有做过像这样的事情了,任何帮助都将不胜感激。我更习惯于先处理这个数据库,而不是先编写代码,我需要一段时间来改变我的思维方式 我有两个实体: public class Councillor { [Key] public int CouncillorId { get; set; } public string Name { get; set; } p

我正在使用EntityFramework6设计一个议员目录,我正在寻找一些关于如何构建我的实体的指导。我已经有一段时间没有做过像这样的事情了,任何帮助都将不胜感激。我更习惯于先处理这个数据库,而不是先编写代码,我需要一段时间来改变我的思维方式

我有两个实体:

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 int councilorid{get;set;}
公共字符串名称{get;set;}
公共ICollection委员会{get;set;}
}
公共班级委员会
{
[关键]
公共int CommitteeId{get;set;}
公共字符串CommitteeName{get;set;}
公共ICollection议员{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; }
}
公共班级委员会
{
[关键]
公共int CommitteeId{get;set;}
公共字符串CommitteeName{get;set;}
公共int主席ID{get;set;}//指向议员实体的链接
public int-ViceChairId{get;set;}//指向议员实体的链接
公共ICollection议员{get;set;}
}

这种方法有什么建议或问题吗?首先,在数据库中,我可能只有一个链接表,用于包含Councilorid、CommitteeId和Position的委员会成员。但是我还不知道如何首先将其转换为代码。

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

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 councillors = from c
                  in Councillors
                  select new
                  {
                      CouncillorName = c.Name,
                      Committes = from cc
                                    in c.CouncilorCommittees
                                    select cc.Committee
                  }

Mike,你是使用代码优先到新数据库还是使用代码优先到现有数据库?代码优先到新数据库,基于有许多问题的旧数据库。看看这个:“避免ORM中的多对多映射”有趣的阅读,基本上停止假装连接/链接表不存在,并对其建模,这使得以后的工作变得容易。Jimmy Bogard(AutoMapper的创建者)对ORMS的总体介绍也非常好:他固执己见,你不必完全同意,但他提出的问题让你很好地理解了如何查看和使用Ormsmun,您的方法与Jason链接到的文章中概述的方法非常相似。所以,基本上,按照正常的数据库开发,坚持一个连接/链接表,并将其视为自己的实体。“我可以这样做,这样就不会让我再去猜测我是如何把这一切放在一起的。”迈克没问题。这就是方法——添加连接表并将其视为自己的实体。我在一些项目中使用过这种方法,效果非常好。
var councillors = from c
                  in Councillors
                  select new
                  {
                      CouncillorName = c.Name,
                      Committes = from cc
                                    in c.CouncilorCommittees
                                    select cc.Committee
                  }