我有一个组件模块模型,可以有许多类型的子模型。所以我宣布像
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CRSApp.API.Models
{
/**
List of Module component in application.!--
Level 0 means main menu
Level 1 means submenu
*/
public class ModuleComponent
{
public int Id { get; set; }
public int DisplayOrder { get; set; }
public string ModuleName { get; set; }
public int Level { get; set; }
public int? ParentId { get; set; }
public ModuleComponent Parent { get; set; }
public ICollection<ModuleComponent> Children {get; set;}
}
}
级别0 ModComponent没有父级(null),但级别1有父级,可能没有子级。 我不知道如何在DataContext中为此模块声明Fluent API。请帮忙
谢谢
注意:我使用asp dotnet core 2.x和ef core 2.0
目前在DataContextDb.OnModelCreating中我只是声明
builder.Entity<ModuleComponent>()
.HasMany(x => x.Children)
.WithOne( x => x.Parent)
.HasForeignKey ( x => x.ParentId)
.OnDelete(DeleteBehavior.Restrict);
够了吗?