LINQ:从链接表中获取数据(多对多关系)

时间:2017-03-20 09:31:31

标签: c# asp.net linq asp.net-core

我有一个ASP.NET Core项目和现有的DB(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; }
}

a db model

问题是我在组件和文档之间有链接表ComponentDocumentationLink(多对多关系),而Component和ComponentType之间没有表(一对一关系)。

当我尝试为控制器中的每个组件获取ComponentType时,我对var components = db.Component.Include(ct => ct.IdComponentTypeNavigation);没有任何问题,但是没有使用此查询从每个组件的文档中检索数据。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

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

然后在一个视图中:

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