【问题标题】:How to define inheritance relationship in LINQ Entity Framework Core如何在 LINQ Entity Framework Core 中定义继承关系
【发布时间】:2020-01-26 11:47:24
【问题描述】:

我正在通过 asp.net core 实现一个项目。在 sql server 中,我有一个名为申请人的表,它有两个子 LegalApplicant 和 PersonApplicant 都继承自申请人表,并且每个都有相关的属性。每个 LegalApplicant 和 PersonApplicant 表都与申请人表具有一对一的关系。我使用 DB first 方法在 Visual Studio 中为它们搭建脚手架。现在我想用linq定义它们之间的关系。如果有人可以解决我的问题,我将不胜感激。我是 EF 核心的新手。甚至我在理解语法方面也有问题。

   public partial class PersonApplicant:Applicant
    {
        public int ApplicantId { get; set; }
        public int ApplicantType { get; set; }
        public string Username { get; set; }
        public string NationalCode { get; set; }
        public string BirthCertificateNo { get; set; }
        public string IssuePlace { get; set; }

        public virtual Applicant Applicant { get; set; }
        public virtual EntityType ApplicantTypeNavigation { get; set; }
    }

public partial class LegalApplicant : Applicant
    {
        public int ApplicantId { get; set; }
        public int ApplicantType { get; set; }
        public string EconomicCode { get; set; }
        public string RegisterNo { get; set; }
        public string NationalCode { get; set; }

        public virtual Applicant Applicant { get; set; }
        public virtual EntityType ApplicantTypeNavigation { get; set; }
    }

 public partial class Applicant
    {
        public Applicant()
        {
            Apiapplicant = new HashSet<Apiapplicant>();
            LegalApplicant = new HashSet<LegalApplicant>();
            PersonApplicant = new HashSet<PersonApplicant>();
        }

        public int ApplicantId { get; set; }
        public int ApplicantType { get; set; }
        public string Address { get; set; }
        public string Description { get; set; }
        public string IsDeleted { get; set; }
        public string Name { get; set; }

        public virtual EntityType ApplicantTypeNavigation { get; set; }
        public virtual ICollection<Apiapplicant> Apiapplicant { get; set; }
        public virtual ICollection<LegalApplicant> LegalApplicant { get; set; }
        public virtual ICollection<PersonApplicant> PersonApplicant { get; set; }
    }

【问题讨论】:

  • 在这里写下你的代码。
  • 我使用 DB first 方法在 Visual Studio 中为它们搭建脚手架。嗯,这也应该定义了关系。

标签: linq asp.net-core inheritance ef-core-3.1


【解决方案1】:

我认为您正在寻找这样的东西:

public class PersonApplicant : Applicant
{
    public string Username { get; set; }
    public string BirthCertificateNo { get; set; }
    public string IssuePlace { get; set; }
}

public class LegalApplicant : Applicant
{
    public string EconomicCode { get; set; }
    public string RegisterNo { get; set; }
}

public class Applicant
{
    public int ApplicantId { get; set; }
    public int ApplicantType { get; set; }
    public string Address { get; set; }
    public string Description { get; set; }
    public string IsDeleted { get; set; }
    public string Name { get; set; }
    public string NationalCode { get; set; }
}

然后,在您的 DbContext 中,执行以下操作:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Applicant>();
}

这将创建一个带有“鉴别器”列的Applicants 表,以区分任何一条记录应使用哪些列。

目前,EF Core 仅支持 table-per-hierarchy (TPH) 模式。 TPH 使用一张表来存储层次结构中所有类型的数据,并使用一个鉴别列来识别每一行代表的类型。

参考:Inheritance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2016-06-30
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多