[英]Entity Framework Joining Three Tables and Using Group Concat
我有三个模型,如下面,
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();
没有你的DbContext和实体映射很难提供帮助,但是我会说你可能只想将Document.DocumentResponsibles
标记为虚拟。
此外,在DocumentResponsible
,您可能想要为Document
添加一个属性,为Team
添加一个属性(也标记为虚拟),这样您就不必一直使用连接键来处理数据,一旦正确配置,EF会为您完成。
如果它不起作用,您可以在您的问题中添加以下信息:首先,上下文类和您拥有的映射。 其次,如果你做var firstDocument = yoneylemDB.Document.First()
, firstDocument
是怎么样的? 它是否填写了所有的字段和属性? 有什么奇怪的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.