C# 我需要一种在请求的记录中包含每个需求的技术(SQL-EF核心)
在我的cms项目中,我创建了一个“RequestGroup”表,用于获取当前“内容”中的所有需求。例如,我的主页“内容”中有一个小部件“内容”,因此在这种情况下,我找不到方法 目前我正在使用EF core来解决这个问题。但EF核心不足以解决这个问题。所以我尝试了sql,但也无法在sql上构建解决方案。需要你的帮助,任何想法都可以。谢谢你抽出时间C# 我需要一种在请求的记录中包含每个需求的技术(SQL-EF核心),c#,sql,sql-server,entity-framework-core,C#,Sql,Sql Server,Entity Framework Core,在我的cms项目中,我创建了一个“RequestGroup”表,用于获取当前“内容”中的所有需求。例如,我的主页“内容”中有一个小部件“内容”,因此在这种情况下,我找不到方法 目前我正在使用EF core来解决这个问题。但EF核心不足以解决这个问题。所以我尝试了sql,但也无法在sql上构建解决方案。需要你的帮助,任何想法都可以。谢谢你抽出时间 public class ContentMain : IdBase { [MaxLength(256)] public string N
public class ContentMain : IdBase
{
[MaxLength(256)]
public string Name { get; set; } = null;
public Guid RequestGroupId { get; set; }
[ForeignKey("DetailPage")]
public Guid? DetailPageId { get; set; } = null;
public virtual VisualizationMain DetailPage { get; set; } = null;
public virtual List<SeoOptionContentMain> SeoOptions { get; set; } = new List<SeoOptionContentMain>();
public string _parameters { get; set; }
[NotMapped]
public Parameters Parameters
{
get
{
try
{
return JsonConvert.DeserializeObject<Parameters>(_parameters);
}
catch
{
return new Parameters();
}
}
set
{
_parameters = JsonConvert.SerializeObject(value);
}
}
[ForeignKey("Category")]
public Guid CategoryId { get; set; }
public virtual CategoryMain Category { get; set; } = null;
public Guid DefinitionId { get; set; }
[NotMapped]
public DefinitionMain Definition { get; set; }
}
public类ContentMain:IdBase
{
[最大长度(256)]
公共字符串名称{get;set;}=null;
公共Guid RequestGroupId{get;set;}
[外键(“详细页”)]
公共Guid?DetailPageId{get;set;}=null;
公共虚拟可视化Main DetailPage{get;set;}=null;
公共虚拟列表SeoOptions{get;set;}=new List();
公共字符串_参数{get;set;}
[未映射]
公共参数
{
得到
{
尝试
{
返回JsonConvert.DeserializeObject(_参数);
}
抓住
{
返回新参数();
}
}
设置
{
_参数=JsonConvert.SerializeObject(值);
}
}
[外键(“类别”)]
公共Guid类别ID{get;set;}
公共虚拟类别主要类别{get;set;}=null;
公共Guid定义ID{get;set;}
[未映射]
公共定义主定义{get;set;}
}
public类RequestGroupMain
{
[关键]
公共Guid Id{get;set;}
[最大长度(256)]
[必需]
公共字符串名称{get;set;}
公共Guid?RequestGroupMainId{get;set;}
公共虚拟IList子项{get;set;}=new List();
公共虚拟列表可视化{get;set;}=new List();
公共虚拟列表内容{get;set;}=new List();
公共虚拟列表类别{get;set;}=new List();
公共虚拟列表类别组{get;set;}=new List();
}
因此,您的问题是自引用父/子关系(RequestGroupMain.Children
)
正如您所发现的,仅使用一个查询对EF core不起作用。你可以用
context.Entry(theRequestGroupMain).Reference(rgm=>rgm.Visualizations.Load())
有一些循环和递归。当然,如果需要在一个请求中获取大量嵌套数据,则可能会导致太多查询
您还可以结合使用一些
Include(rgm=>rgm.Children)。然后Include(…)
直到某个深度,然后通过多次调用ReferenceEntry.Load()
进一步深入
如果这不可行(例如出于性能原因),您仍然可以选择“原始SQL”路径。为此,您的查询需要使用“递归公共表表达式(CTE)”。只需在google上搜索“递归CTE父子层次SQL server”之类的内容即可。您能描述一下您的确切问题吗?这很难理解。当然,当我在请求过程中导入“内容”记录时,我希望包括该记录的要求。需求可以是“可视化”、“内容”、“类别”、“类别组”,所以我无法理解。那么context.ContentMain.Include(cm=>cm.visualizationos)Include(cm=>cm.Contents).Include(…
)呢?当然你已经尝试过了,但有什么不起作用呢?我之所以没有尝试,是因为“RequestGroup”我的意思是我使用Guid requestGroupId=context.ContentMain.FirstOrDefault(x=>x.Id.Equals({Guid})).requestGroupId;context.RequestGroupMain.Include(x=>x.RequestGroupMain).FirstOrDefault(x=>x.Id.Equals(requestGroupId));这是我的问题,当我得到“RequestGroup”时,它包括了“Children”,而“Children”是无限嵌套的“RequestGroup”…我希望我能解释一下。其他一些例子,无穷大分类技术在EF core上不起作用。这就是我需要的,非常感谢。因为这种情况,我改变了系统。但是你的解决方案是这个问题的正确答案。
public class RequestGroupMain
{
[Key]
public Guid Id { get; set; }
[MaxLength(256)]
[Required]
public string Name { get; set; }
public Guid? RequestGroupMainId { get; set; }
public virtual IList<RequestGroupMain> Childen { get; set; } = new List<RequestGroupMain>();
public virtual List<VisualizationRequestGroup> Visualizations { get; set; } = new List<VisualizationRequestGroup>();
public virtual List<ContentRequestGroup> Contents { get; set; } = new List<ContentRequestGroup>();
public virtual List<CategoryRequestGroup> Categories { get; set; } = new List<CategoryRequestGroup>();
public virtual List<CategoryGroupRequestGroup> CategoryGroups { get; set; } = new List<CategoryGroupRequestGroup>();
}