首页 > 解决方案 > Entity Framework Core 3.1 嵌套集合删除对象 Linq 表达式失败

问题描述

我的问题对此微笑,我想从嵌套集合中删除项目。 Entity Framework Core,从嵌套集合中删除项目 你能帮我找出我做错了什么吗?谢谢你。

我的数据库更新功能如下所示:

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;
        }

我正在做与接受的答案相同的事情,但它失败了,但有以下例外:

    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.

这是我的模型:

 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; }
        }

标签: c#entity-framework-core

解决方案


尝试将您的missingNodes 查询更改为以下内容:

var missingNodes = context.Nodes
    .Where(i => i.StepId == entity.Id)
    .Except(context.Nodes.Where(n => entity.Nodes.Select(ei => ei.Id).Contains(n.Id)));

或者:

var missingNodes = context.Nodes
        .Where(i => i.StepId == entity.Id && !entity.Nodes.Select(ei => ei.Id).Contains(i.Id));

简而言之,问题是在转换为 SQL 和执行期间(至少在某些情况下),EF 无法将复杂类型的本地集合传递给查询。


推荐阅读