描述
我认为这是一个 EF Core 6.0 错误,但我想在向 EF Core GitHub 存储库提交错误之前检查此处。
我完全不确定这是否是一个错误。至少我认为这是一种意想不到的行为。
在迁移到 .NET 6 后,此行为首次出现。我已成功在由下面所示的 3 个文件组成的简单项目中重现该问题。首先,我认为这是一个重大变化,所以我回顾了所有这些。但是,我无法找到任何会导致此错误的重大更改。由于我无法将错误与任何更改联系起来,因此我开始更改我的 EF Core 版本.csproj
文件。一旦我将所有包(EF Core)更改为 6.0.0-rc.2.21480.5,错误就不会再出现。
另一件要强调的事情是,如果我添加一个SaveChanges
删除program.cs 文件后,该错误也不会发生。
您认为这是一个错误还是我错过了新功能或重大更改?
Person.cs
:
#nullable disable
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
public Person Parent { get; set; }
protected Person()
{
}
public Person(string name, Person parent)
{
this.Name = name;
if (parent is not null)
{
this.Parent = parent;
}
}
}
Context.cs
:
#nullable disable
using Microsoft.EntityFrameworkCore;
public class Context : DbContext
{
public Context()
{}
public DbSet<Person> Parents { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
"Data Source=DES04;Initial Catalog=Test;Integrated Security=true");
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.HasOne(p => p.Parent)
.WithMany()
.IsRequired(false)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Person>()
.HasKey(p => p.PersonId);
}
}
Program.cs
:
var context = new Context();
var parent = new Person("1", null);
var child1 = new Person("2", parent);
var child2 = new Person("3", parent);
var grandchild1 = new Person("4", child1);
var grandchild2 = new Person("5", child1);
var grandchild3 = new Person("6", child2);
var grandchild4 = new Person("7", child2);
context.Add(parent);
context.Add(child1);
context.Add(child2);
context.Add(grandchild1);
context.Add(grandchild2);
context.Add(grandchild3);
context.Add(grandchild4);
context.SaveChanges();
context.Remove(parent);
context.Remove(child1);
context.Remove(child2);
context.Remove(grandchild1);
context.Remove(grandchild2);
context.Remove(grandchild3);
context.Remove(grandchild4);
parent = new Person("1", null);
child1 = new Person("2", parent);
child2 = new Person("3", parent);
grandchild1 = new Person("4", child1);
grandchild2 = new Person("5", child1);
grandchild3 = new Person("6", child2);
grandchild4 = new Person("7", child2);
context.Add(parent);
context.Add(child1);
context.Add(child2);
context.Add(grandchild1);
context.Add(grandchild2);
context.Add(grandchild3);
context.Add(grandchild4);
context.SaveChanges();
堆栈跟踪:
未处理的异常。 System.InvalidOperationException:无法保存更改,因为在要保存的数据中检测到循环依赖关系:'Person [Deleted]Person [Deleted] Parent { 'ParentId' }
在 Microsoft.EntityFrameworkCore.Utilities.Multigraph2.ThrowCycle(List
1个周期,功能2 formatCycle, Func
2 格式异常)
在 Microsoft.EntityFrameworkCore.Utilities.Multigraph2.BatchingTopologicalSort(Func
4 尝试BreakEdge、Func2 formatCycle) at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.TopologicalSort(IEnumerable
1 个命令)
在 Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.BatchCommands(IList1 entries, IUpdateAdapter updateAdapter)+MoveNext() at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable
1个commandBatches,IRelationalConnection连接)
在 Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList1 entries) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList
1 条待保存)
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager,布尔acceptAllChangesOnSuccess)
在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.c.b__104_0(DbContext _, ValueTuple2 t) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func
3操作,Func`3验证成功)
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(布尔acceptAllChangesOnSuccess)
在Microsoft.EntityFrameworkCore.DbContext.SaveChanges(布尔acceptAllChangesOnSuccess)
在 Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
在 C:\Users\Dev04\Desktop\Nueva Carpeta\test\Program.cs 中的 Program.$(String[] args) 处:第 44 行
版本信息
- EF 核心:
Microsoft.EntityFrameworkCore
6.0.0
- 数据库提供商:
Microsoft.EntityFrameworkCore.SqlServer
6.0.0
- 目标框架:.NET 6.0.0