簡體   English   中英

LINQ:從鏈接表中獲取數據(多對多關系)

[英]LINQ: Get data from linked tables (many-to-many relation)

我有一個ASP.NET Core項目和現有的數據庫(MS SQLServer)。 我已經有來自db的腳手架模型:

Component.cs

public partial class 
{
    public Component()
    {
        ComponentDocumentationLink = new HashSet<ComponentDocumentationLink>();

    }

    public int IdConponent { get; set; }
    public string ComponentName { get; set; }
    public string ComponentTitle { get; set; }
    public string ComponentDescription { get; set; }
    public int IdComponentType { get; set; }
    public int IdrecStatus { get; set; }
    public DateTime CreateDate { get; set; }
    public string CreateUser { get; set; }
    public string ChangeUser { get; set; }
    public DateTime? ChangeDate { get; set; }


    public virtual ICollection<ComponentDocumentationLink> ComponentDocumentationLink { get; set; }
    public virtual ComponentType IdComponentTypeNavigation { get; set; }

}

ComponentType.cs

public partial class ComponentType
{
    public ComponentType()
    {
        Component = new HashSet<Component>();
    }

    public int IdComponentType { get; set; }
    public string ComponentTypeName { get; set; }
    public int IdrecStatus { get; set; }
    public DateTime CreateDate { get; set; }
    public string CreateUser { get; set; }
    public string ChangeUser { get; set; }
    public DateTime? ChangeDate { get; set; }

    public virtual ICollection<Component> Component { get; set; }
}

ComponentDocumentationLink.cs

public partial class ComponentDocumentationLink
{
    public int IdComponentDocumentationLink { get; set; }
    public int IdComponent { get; set; }
    public int IdDocumentation { get; set; }
    public int IdrecStatus { get; set; }
    public DateTime CreateDate { get; set; }
    public string CreateUser { get; set; }
    public string ChangeUser { get; set; }
    public DateTime? ChangeDate { get; set; }

    public virtual Component IdComponentNavigation { get; set; }

    public virtual Documentation IdDocumentationNavigation { get; set; }


}

和Documentation.cs

public partial class Documentation
{
    public Documentation()
    {
        ComponentDocumentationLink = new HashSet<ComponentDocumentationLink>();
    }

    public int IdDocumentation { get; set; }
    public int IdDocumentationType { get; set; }
    public string DocumentationSrc { get; set; }
    public int IdrecStatus { get; set; }
    public DateTime CreateDate { get; set; }
    public string CreateUser { get; set; }
    public string ChangeUser { get; set; }
    public DateTime? ChangeDate { get; set; }

    public virtual ICollection<ComponentDocumentationLink> ComponentDocumentationLink { get; set; }
    public virtual DocumentationType IdDocumentationTypeNavigation { get; set; }
}

數據庫模型

問題是我在組件和文檔之間有鏈接表ComponentDocumentationLink(多對多關系),而在Component和ComponentType之間沒有表(一對一關系)。

當我嘗試獲取控制器中每個組件的ComponentType時,我對var components = db.Component.Include(ct => ct.IdComponentTypeNavigation);沒問題var components = db.Component.Include(ct => ct.IdComponentTypeNavigation); ,但沒有使用此查詢從文檔中檢索每個組件的數據。

我找到了解決方案。

db.Something.Include(x => x.SomethingJunction).ThenInclude(x => x.JunctionedTable);

然后來看:

@var a = item.SomethinJunction.Select(x => x.JunctionedTable);
@a.Property;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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