【问题标题】:LINQ: Get data from linked tables (many-to-many relation)LINQ:从链接表中获取数据(多对多关系)
【发布时间】:2017-03-20 09:31:31
【问题描述】:

我有一个 ASP.NET Core 项目和现有数据库 (MS SQLServer)。我已经有来自 db 的脚手架模型:

组件.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 =&gt; ct.IdComponentTypeNavigation); 没有问题,但没有使用此查询从文档中检索每个组件的数据。

【问题讨论】:

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


    【解决方案1】:

    我找到了解决办法。

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

    然后在一个视图中:

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

    【讨论】:

      猜你喜欢
      • 2015-08-27
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      相关资源
      最近更新 更多