标签:virt 引用 with 基本 foreign sql public cti nosql
机械M由多个零件C组成
不同的零件有不同的属性,所以有C1,C2等等
由于零件的差异化,导致C1、C2不能通过统一的表C来表示
同时设计接口InterfaceC作为零件表的接口,整合一些统一的内容
综上所述,有表M,C1,C2和接口InterfaceC,C1、C2实现InterfaceC
在M表中,定义
public virtual ICollection<InterfaceC> CList { get; set; }
在C1、C2表中定义
public int MId { get; set; }
public virtual M M { get; set; }
在M中配置
modelBuilder.Entity<M>().HasMany(x => x.CList)
.WithOne(x => x.M)
.HasForeignKey(x => x.MId);
报错:
The specified type ‘InterfaceC‘must be a non-interface reference type to be used as an entity type .
即ef core不能接受接口引用类型作为实体类型
TPT=每个实际对象一个表
在M表中定义两个集合, 两个集合对应C1、C2表的两个外键
public virtual ICollection<C1> C1List { get; set; }
public virtual ICollection<C2> C2List { get; set; }
在C1、C2中分别配置
modelBuilder.Entity<C1>().HasOne(x => x.M)
.WithMany(x => x.C1List)
.HasForeignKey(x => x.MId);
modelBuilder.Entity<C2>().HasOne(x => x.M)
.WithMany(x => x.C2List)
.HasForeignKey(x => x.MId);
就ok啦
当然这种数据结构还是推荐使用nosql,mongo的方式
这里只是刚好讨论到ef core~
EntityFrameworkCore 一表对多表存在外键的设计
标签:virt 引用 with 基本 foreign sql public cti nosql
原文地址:https://www.cnblogs.com/Lulus/p/12163333.html