Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据数据库表从创建Xml时出错:.Net Core 3.1内存泄漏_C#_Xml_Entity Framework_Asp.net Core 2.1_Asp.net Core 3.1 - Fatal编程技术网

C# 根据数据库表从创建Xml时出错:.Net Core 3.1内存泄漏

C# 根据数据库表从创建Xml时出错:.Net Core 3.1内存泄漏,c#,xml,entity-framework,asp.net-core-2.1,asp.net-core-3.1,C#,Xml,Entity Framework,Asp.net Core 2.1,Asp.net Core 3.1,我正在设置一种获取Xml数据的方法,这些数据来自数据库表。上述功能一直在工作,直到.Net Core2.1项目更新为.Net Core 3.1.1。 下面的代码显示了错误 Template.cs orderTemplate = (from ordt in _context.OrderTemplates where ordt.Id == orderTempId // int orderTempId=1;

我正在设置一种获取Xml数据的方法,这些数据来自数据库表。上述功能一直在工作,直到
.Net Core2.1
项目更新为
.Net Core 3.1.1
。 下面的代码显示了错误

Template.cs

 orderTemplate =
                  (from ordt in _context.OrderTemplates
                   where ordt.Id == orderTempId // int orderTempId=1;
                   select new XElement("OrderTemplate",
                          new XElement("OrderTemplateId", ordt.Id),
                          new XElement("OrderTemplateName", ordt.Name),
                          new XElement("OrderTemplateDescription", ordt.Description)));
例外情况

Client projection contains reference to constant expression of 'System.Xml.Linq.XName'. 
This could potentially cause memory leak. Consider assigning this constant to local variable 
and using the variable in the query instead. See https://go.microsoft.com/fwlink/?linkid=2103067 for more information.
XML数据格式:(预期输出)


已经知道这是一个重复的问题,但我在这里做的编码有什么错误。请指导我。

在将实体投影到XML之前,只需选择实体即可。这就是EF Core 2.1所做的

 var orderTemplates =_context.OrderTemplates.Where(ordt=> ordt.Id == orderTempId).AsEnumerable();

 var orderTemplate =
                  (from ordt in orderTemplates 
                   select new XElement("OrderTemplate",
                          new XElement("OrderTemplateId", ordt.Id),
                          new XElement("OrderTemplateName", ordt.Name),
                          new XElement("OrderTemplateDescription", ordt.Description))).ToList();
从概念上讲,第一行代码构建查询,将其转换为SQL,运行SQL查询并返回可以使用结果的IEnumerable


第二行将查询结果枚举为
orderTemplate
对象,并将其投影到XElements中,然后将其复制到列表中。

您可能需要阅读以下内容:@jdweng已通过..这可能是错误:LINQ查询不再在客户端上进行评估,并且也已对此进行检查。我同意这是根本原因,但不一定是真正的原因。我认为模型可能需要更新。该错误似乎暗示orderTempId应该是一个参数:其中ordt.Id==orderTempId。因此,如果查询现在是一个存储过程(在服务器上而不是客户端上运行),那么变量应该是一个参数。像这样的实现来自Api的ordetTempId,通过表ORDERTEM,基于此表的id,采取的操作表等等。Api与Core 3.0+兼容吗?如果有n级呢?逻辑完全相同。您只需将
AsEnumerable()
放在
的where
后面,即可控制服务器端和客户端评估之间的界限。谢谢@David Browne-Microsoft,您是否介意解释或提供任何支持链接?
 orderTemplate =
                  (from ordt in _context.OrderTemplates
                   where ordt.Id == orderTemplateId
                  select new {
                  _OrderTemplate = ordt,
                 _OperationTemplates =(from ot in _context.OrderTemplates                                                
                                       join opt in _context.OperationTemplates 
                                     on ot.Id equals opt.OrderTemplateId into ps1                                               
                                    from opt in ps1.DefaultIfEmpty()
                                    join asm in _context.AssemblyUnits
                                    on opt.AssemblyUnitId equals asm.Id into ps5
                                   from asm in ps5.DefaultIfEmpty()
                                    where ot.Id == ordt.Id
                                      orderby opt.OrderBy ascending
                                      select new
                                                {
                                                    _OperationTemplate =  opt,
                                                    _Assembly =  asm,
})}).AsEnumerable();
 var orderTemplates =_context.OrderTemplates.Where(ordt=> ordt.Id == orderTempId).AsEnumerable();

 var orderTemplate =
                  (from ordt in orderTemplates 
                   select new XElement("OrderTemplate",
                          new XElement("OrderTemplateId", ordt.Id),
                          new XElement("OrderTemplateName", ordt.Name),
                          new XElement("OrderTemplateDescription", ordt.Description))).ToList();