一对多的ef核心共享子表

时间:2020-07-16 05:02:22

标签: ef-code-first ef-core-2.2

我想要一个共享表,该共享表可以以某种方式引用多个父表。父表可以包含共享附件表中的多行。 Type / TypeId将是指向父表的列(例如,如果parentId在“人”或“公司”表中) 共享表:

Id   | ParentId   | Type/TypeId   |   Value
1         1            Person/1    "somestringvalue"
2         1            Person/1    "another value"
3         3            Company/2   "value"
....

模型看起来像这样

public class Attachement
{
    [Key]
    public int Id { get; set; }
    //PK- Id of the parent table
    public int ParentId { get; set; }
    // type or enum of a parent table. Should point to which table it points to
    public int Type { get; set; }

    public string Value { get; set; }
}

public class Person
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
    //shared table
    public ICollection<Attachement> Attachements { get; set; }
}

public class Company
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
    //shared table
    public ICollection<Attachement> Attachements { get; set; }
}

侧面注释-我使用代码优先模式。

谢谢!

0 个答案:

没有答案