C# 实体框架核心3.1嵌套集合删除对象Linq表达式失败
我的问题是,我想从嵌套集合中删除项。 你能帮我找出我做错了什么吗? 多谢各位 My DB update函数如下所示:C# 实体框架核心3.1嵌套集合删除对象Linq表达式失败,c#,entity-framework-core,C#,Entity Framework Core,我的问题是,我想从嵌套集合中删除项。 你能帮我找出我做错了什么吗? 多谢各位 My DB update函数如下所示: public async Task<Step> Update(short id, Step entity) { entity.Id = id; var context = _formulaDBContext; var missingNodes = context.Nodes.Whe
public async Task<Step> Update(short id, Step entity)
{
entity.Id = id;
var context = _formulaDBContext;
var missingNodes = context.Nodes.Where(i => i.StepId == entity.Id).Except(entity.Nodes);
context.Nodes.RemoveRange(missingNodes);
var t = Task.Run(() => context.Set<Step>().Update(entity));
t.Wait();
await context.SaveChangesAsync();
return entity;
}
var missingNodes = context.Nodes
.Where(i => i.StepId == entity.Id)
.Except(context.Nodes.Where(n => entity.Nodes.Select(ei => ei.Id).Contains(n.Id)));
公共异步任务更新(短id,步骤实体)
{
实体Id=Id;
var context=_formulaDBContext;
var missingNodes=context.Nodes.Where(i=>i.StepId==entity.Id)。除了(entity.Nodes);
context.Nodes.RemoveRange(missingNodes);
var t=Task.Run(()=>context.Set().Update(entity));
t、 等待();
wait context.saveChangesSync();
返回实体;
}
我所做的与公认的答案相同,但它失败了,例外情况如下:
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (Processing of the LINQ expression 'DbSet<Node>
.Where(i => (int)i.StepId == (int)__entity_Id_0)
.Except(__p_1)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.)
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at Formula.UI.RecepiDataViewModel.<EditStep>d__8.MoveNext() in C:\Users\gje\source\repos\formula\Formula.UI\ViewModels\RecepiMaker\RecepiDataViewModel.cs:line 62
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Formula.UI.RecepiDataViewModel.<<-ctor>b__7_1>d.MoveNext() in C:\Users\gje\source\repos\formula\Formula.UI\ViewModels\RecepiMaker\RecepiDataViewModel.cs:line 46
This exception was originally thrown at this call stack:
[External Code]
Formula.UI.StepDataService.Update(short, Formula.Models.Step) in StepDataService.cs
Inner Exception 1:
InvalidOperationException: Processing of the LINQ expression 'DbSet<Node>
.Where(i => (int)i.StepId == (int)__entity_Id_0)
.Except(__p_1)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
System.aggregateeexception
HResult=0x80131500
消息=发生了一个或多个错误。(LINQ表达式“DbSet”的处理
。其中(i=>(int)i.StepId==(int)\u实体\u Id\u 0)
。由“NavigationExpandingExpressionVisitor”执行的除(u p_1)之外的操作失败。这可能表示EF Core中存在错误或限制。请参阅https://go.microsoft.com/fwlink/?linkid=2101433 有关更多详细信息。)
Source=System.Private.CoreLib
堆栈跟踪:
位于System.Threading.Tasks.Task.ThrowifeException(布尔值IncludeTaskCanceledException)
在System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken CancellationToken)
在System.Threading.Tasks.Task.Wait()中
在C:\Users\gje\source\repos\Formula\Formula.UI\ViewModels\ReceipMaker\ReceipDataViewModel.cs中的Formula.UI.ReceipDataViewModel.d_u8.MoveNext()处:第62行
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter.GetResult()中
在C:\Users\gje\source\repos\Formula\Formula.UI\ViewModels\receipimaker\receipidataviewmodel.cs中的Formula.UI.receipidataviewmodel.d.MoveNext()处:第46行
此异常最初是在此调用堆栈中引发的:
[外部代码]
StepDataService.cs中的Formula.UI.StepDataService.Update(缩写为Formula.Models.Step)
内部异常1:
InvalidOperationException:处理LINQ表达式“DbSet”
。其中(i=>(int)i.StepId==(int)\u实体\u Id\u 0)
。由“NavigationExpandingExpressionVisitor”执行的(u p_1)除外失败。这可能表明EF核心中存在缺陷或限制。看见https://go.microsoft.com/fwlink/?linkid=2101433 更多详细信息。
这是我的模型:
public class Step : BaseModel
{
public Step()
{
Nodes = new List<Node>();
}
public short RecepiId { get; set; }
public short PVTagId { get; set; }
public Operator Operator { get; set; }
public TagMetaData PVTag { get; set; }
public string Value { get; set; }
public Activity Activity { get; set; }
[ForeignKey("StepId")]
public List<Node> Nodes { get; set; }
}
public class Step : BaseModel
{
public Step()
{
Nodes = new List<Node>();
}
public short RecepiId { get; set; }
public short PVTagId { get; set; }
public Operator Operator { get; set; }
public TagMetaData PVTag { get; set; }
public string Value { get; set; }
public Activity Activity { get; set; }
[ForeignKey("StepId")]
public List<Node> Nodes { get; set; }
}
public class BaseModel
{
public short Id { get; set; }
public string Name { get; set; }
}
公共类步骤:BaseModel
{
公共步骤()
{
节点=新列表();
}
公共短recepid{get;set;}
公共短PVTagId{get;set;}
公共运算符{get;set;}
公共标记元数据PVTag{get;set;}
公共字符串值{get;set;}
公共活动活动{get;set;}
[外键(“StepId”)]
公共列表节点{get;set;}
}
公共类步骤:BaseModel
{
公共步骤()
{
节点=新列表();
}
公共短recepid{get;set;}
公共短PVTagId{get;set;}
公共运算符{get;set;}
公共标记元数据PVTag{get;set;}
公共字符串值{get;set;}
公共活动活动{get;set;}
[外键(“StepId”)]
公共列表节点{get;set;}
}
公共类基模型
{
公共短Id{get;set;}
公共字符串名称{get;set;}
}
尝试将缺失节点的查询更改为以下内容:
public async Task<Step> Update(short id, Step entity)
{
entity.Id = id;
var context = _formulaDBContext;
var missingNodes = context.Nodes.Where(i => i.StepId == entity.Id).Except(entity.Nodes);
context.Nodes.RemoveRange(missingNodes);
var t = Task.Run(() => context.Set<Step>().Update(entity));
t.Wait();
await context.SaveChangesAsync();
return entity;
}
var missingNodes = context.Nodes
.Where(i => i.StepId == entity.Id)
.Except(context.Nodes.Where(n => entity.Nodes.Select(ei => ei.Id).Contains(n.Id)));
或:
简言之,问题在于EF无法在转换为SQL和执行期间(至少在某些情况下)将复杂类型的本地集合传递给查询