【问题标题】:Entity Framework Core 3.1 Nested Collections Remove Objects Linq Expression FailsEntity Framework Core 3.1 嵌套集合删除对象 Linq 表达式失败
【发布时间】:2020-06-26 21:00:06
【问题描述】:

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

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

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


    【解决方案1】:

    尝试将您的 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));
    

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

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2021-10-19
      相关资源
      最近更新 更多