簡體   English   中英

實體框架加入三個表並使用組Concat

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM