.net core EF Core 2.0用于自参考表格/模型
我有一个组件模块模型,它可以有许多它类型的子模块。所以我宣布喜欢.net core EF Core 2.0用于自参考表格/模型,.net-core,entity-framework-core,.net Core,Entity Framework Core,我有一个组件模块模型,它可以有许多它类型的子模块。所以我宣布喜欢 using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace CRSApp.API.Models { /** List of Module component in application
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;}
}
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
命名空间CRSApp.API.Models
{
/**
应用程序中的模块组件列表--
级别0表示主菜单
级别1表示子菜单
*/
公共类模块组件
{
公共int Id{get;set;}
公共int显示顺序{get;set;}
公共字符串ModuleName{get;set;}
公共整数级别{get;set;}
public int?ParentId{get;set;}
公共模块组件父级{get;set;}
公共ICollection子项{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);
builder.Entity()
.HasMany(x=>x.Children)
.WithOne(x=>x.Parent)
.HasForeignKey(x=>x.ParentId)
.OnDelete(DeleteBehavior.Restrict);
足够了吗?您是否得到了异常或意外行为?从建模的角度来说足够了。需要特殊代码才能删除。