在Entity Framework Core中建模文件夾結構

c# entity-framework entity-framework-core sql-server

我想將分層文件夾結構保存到SQL數據庫中。這堂課是這樣的:

public class Folder
{
    public Folder()
    {
        Children = new List<Folder>();
    }

    public string Name { get; set;  }
    public int Id { get; set; }

    public int? ParentId { get; set; }
    public Folder Parent { get; set; }

    public ICollection<Folder> Children { get; set; }
}

我正在嘗試使用Entity Framework Core映射它:

builder.Entity<Folder>()
       .HasKey(i => i.Id);

// Relation 1
builder.Entity<Folder>()
       .HasMany(e => e.Children)
       .WithOne(e => e.Parent)
       .HasForeignKey(e => e.ParentId);

// Relation 2
builder.Entity<Folder>()
       .HasOne(f => f.Parent)
       .WithMany(f => f.Children)
       .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Cascade);

如果我嘗試更新數據庫,我會收到以下異常:

System.Data.SqlClient.SqlException:在表'文件夾'上引入FOREIGN KEY約束'FK_Folders_Folders_ParentId'可能會導致循環或多個級​​聯路徑。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY約束。無法創建約束或索引。查看以前的錯誤。
在System.Data.SqlClient.SqlConnection.OnError(SqlException異常,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)
在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
在Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection連接,String executeMethod,IReadOnlyDictionary 2 parameterValues, Boolean openConnection, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary
2 parameterValues,布爾manageConnection)
在Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands,IRelationalConnection連接)
在Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
在Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration,String contextType)
在Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand。<> c__DisplayClass0_0.b__0()
在Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String [] args)
在Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String [] args)

ClientConnectionId:f0c08167-fba7-4afa-baf0-45909e9a1f4b
錯誤號碼:1785,狀態:0,等級:16

在表'文件夾'上引入FOREIGN KEY約束'FK_Folders_Folders_ParentId'可能會導致循環或多個級​​聯路徑。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY約束。無法創建約束或索引。查看以前的錯誤。

我也嘗試在沒有'Relation 2'的情況下映射它,它可以工作,但是當我從數據庫加載項目時,它們返回單項,但未設置Children屬性。

存儲此類數據的正確方法是什麼?

一般承認的答案

您不需要任何映射配置。模型定義本身足以用於遷移以生成正確的表結構,該結構將是名為Id的PK字段,以及名為ParentId的可空FK字段。

EF Core中的主要原理與EF 6相同。欲了解更多信息,請訪問: http//www.mikesdotnetting.com/article/255/entity-framework-recipe-hierarchical-data-management



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow