【问题标题】:Entity Framework Joining Three Tables and Using Group Concat实体框架连接三个表并使用组 Concat
【发布时间】:2017-10-06 23:34:39
【问题描述】:

我有如下三个模型,

    public class Team
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class Document
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string Application { get; set; }
        public ICollection<DocumentResponsible> DocumentResponsibles { get; set; }
        public string Pcda { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }
    public class DocumentResponsible
    {
        public int Id { get; set; }
        public int DocumentId { get; set; }
        public int TeamId { get; set; }
    }

我想编写一个实体框架表达式来连接三个表,并在每个文档的一行中选择文档表和团队名称的所有字段。所以基本上我想加入三个表使用 group_concat 作为团队名称。然后我想将它绑定到 web 表单中的 gridview。

我尝试过的,

(from dc in DBContext.Document
join dr in DBContext.DocumentResponsible on dc.Id equals dr.DocumentId
join t in DBContext.Team on dr.TeamId equals t.Id
select new
{
    Name = dc.Name,
    Type = dc.Type,
    Application = dc.Application,
    Pcda = dc.Pcda,
}).ToList();

我刚刚试过了,

var data = DBContext.Dcoument.Include("DocumentResponsibles").Tolist();

【问题讨论】:

  • “DcoumentResponsibles”应该这样拼写吗?它似乎不符合您的其余命名。
  • 谢谢,我打错了。

标签: c# asp.net entity-framework webforms


【解决方案1】:

没有您的 DbContext 和实体映射很难提供帮助,但我会说您可能只想将 Document.DocumentResponsibles 标记为虚拟。

另外,在DocumentResponsible 中,也许您想为Document 添加一个属性,为Team 添加一个属性(两者都标记为虚拟),这样您就不必在所有过程中都使用连接键当您想处理数据时,EF 会在正确配置后为您完成。

如果不起作用,您能否将以下信息添加到您的问题中:首先,上下文类和您拥有的映射。其次,如果你做var firstDocument = yoneylemDB.Document.First()firstDocument 看起来怎么样?它是否填写了所有字段和属性?有什么奇怪的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多