C# LINQ:从链接表(多对多关系)获取数据
我有一个ASP.NET核心项目和现有的数据库(MS SQLServer)。我已经从db获得了脚手架模型: Component.csC# LINQ:从链接表(多对多关系)获取数据,c#,asp.net,linq,asp.net-core,C#,Asp.net,Linq,Asp.net Core,我有一个ASP.NET核心项目和现有的数据库(MS SQLServer)。我已经从db获得了脚手架模型: Component.cs public partial class { public Component() { ComponentDocumentationLink = new HashSet<ComponentDocumentationLink>(); } public int IdConponent { get; set
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; }
}
和文件.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=新哈希集();
}
公共int IdDocumentation{get;set;}
public int IdDocumentationType{get;set;}
公共字符串文档src{get;set;}
public int idrroidus{get;set;}
公共日期时间CreateDate{get;set;}
公共字符串CreateUser{get;set;}
公共字符串ChangeUser{get;set;}
公共日期时间?更改日期{get;set;}
公共虚拟ICollection组件DocumentationLink{get;set;}
公共虚拟文档类型IdDocumentationTypeNavigation{get;set;}
}
问题是我在组件和文档之间有链接表ComponentDocumentation链接(多对多关系),而在组件和ComponentType之间没有表(一对一关系)
当我试图为控制器中的每个组件获取ComponentType时,我对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;
db.Something.Include(x => x.SomethingJunction).ThenInclude(x => x.JunctionedTable);
@var a = item.SomethinJunction.Select(x => x.JunctionedTable);
@a.Property;