C# EF Core DeleteBehavior.SetNull产生循环问题

C# EF Core DeleteBehavior.SetNull产生循环问题,c#,.net-core,entity-framework-core,C#,.net Core,Entity Framework Core,我使用的是EF Core 1.1.0,我的级联行为存在严重问题 我有一个叫“像贝娄一样的陆地”的模型: public class Land { public long Id { get; set; } public int HorizontalPosition { get; set; } public int VerticalPosition { get; set; } public bool IsPlaced { get; set; } // Rela

我使用的是EF Core 1.1.0,我的级联行为存在严重问题

我有一个叫“像贝娄一样的陆地”的模型:

public class Land
{
    public long Id { get; set; }
    public int HorizontalPosition { get; set; }
    public int VerticalPosition { get; set; }
    public bool IsPlaced { get; set; }

    // Relations
    public string UserId { get; set; }
    public virtual User User { get; set; }
    public long? BuildingId { get; set; }
    public virtual Building Building { get; set; }
}
还有另一个模型叫做这样的建筑:

public class Building
{
    public long Id { get; set; }
    public bool IsPermanent { get; set; }
    public int UpgradeCount { get; set; }

    // Relations
    public string UserId { get; set; }
    public virtual User User { get; set; }
    public int BuildingTypeId { get; set; }
    public virtual BuildingType BuildingType { get; set; }
    public virtual List<Land> Lands { get; set; }
}
...
migrationBuilder.AddForeignKey(
            name: "FK_Lands_Buildings_BuildingId",
            table: "Lands",
            column: "BuildingId",
            principalTable: "Buildings",
            principalColumn: "Id",
            onDelete: ReferentialAction.SetNull);
...
我已经告诉过你,在删除时将外键设置为null,但它不希望我做任何操作。尽管在dbcontext中没有称为“NoAction”的行为


您看到的异常文本是由SQL Server生成的

EF Core没有任何等效的
无操作

唯一的方法是将
限制为一个引用(或两个引用)


在删除父实体(
User
)之前,您需要手动管理相关记录(例如更新或删除
Lands
)。或者,在呈现视图时检查子记录是否存在,并且不显示用于删除用户的链接/按钮。或者在删除过程中捕获异常,中止操作并向用户显示相关消息。

在EF Core 2.2.6版中,Microsoft.EntityFrameworkCore.DeleteBehavior
枚举中有
ClientSetNull
条目

在我的案例中,使用
ClientSetNull
而不是
SetNull
产生了不同


我不明白为什么MsSqlServer不能解析多个级联路径,其他引擎完全可以这样做。

我不认为
SetNull
是相关的(同样的情况也会发生在
cascade
)。该循环很可能是由两个实体中的
User
FK的级联删除引起的。
System.Data.SqlClient.SqlException: Introducing FOREIGN KEY constraint 'FK_Lands_Buildings_BuildingId' on table 'Lands' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:65b8e08e-2d17-46ac-b734-385f88dd07b1
Error Number:1785,State:0,Class:16
Introducing FOREIGN KEY constraint 'FK_Lands_Buildings_BuildingId' on table 'Lands' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
...
migrationBuilder.AddForeignKey(
            name: "FK_Lands_Buildings_BuildingId",
            table: "Lands",
            column: "BuildingId",
            principalTable: "Buildings",
            principalColumn: "Id",
            onDelete: ReferentialAction.SetNull);
...