.net core .Net核心和实体框架核心-使用每个层次结构的表访问模型中子类的属性

.net core .Net核心和实体框架核心-使用每个层次结构的表访问模型中子类的属性,.net-core,entity-framework-core,abstract-class,table-per-hierarchy,.net Core,Entity Framework Core,Abstract Class,Table Per Hierarchy,我对.NETCore&Entity框架还很陌生。我正在从事一个.Net核心项目,该项目有一个使用代码优先方法创建的数据库。模型伪代码中存在以下继承结构: public abstract class LegalEntity{ // common properties public virtual LegalEntityDto ToDto() { return null; } } public class Person : LegalEntity { publi

我对.NETCore&Entity框架还很陌生。我正在从事一个.Net核心项目,该项目有一个使用代码优先方法创建的数据库。模型伪代码中存在以下继承结构:

public abstract class LegalEntity{
// common properties
   public virtual LegalEntityDto ToDto()
   {
      return null;
   }
}

public class Person : LegalEntity
{
   public string FirstName {get; private set;}
   public DateTime? DateOfBirth {get; private set;}
   // ... Other person-specific properties.
   public new PersonDto ToDto()
   {
      return new PersonDto
      {
         {
         Firstname = Firstname,
         DateOfBirth = DateOfBirth
         // ...
         };
      }

public class Company : LegalEntity
    {
       public string Abn {get; private set;}
       public string CompanyName {get; private set;}
       // ... Other company-specific properties.
       public new CompanyDto ToDto()
       {
          return new CompanyDto
          {
             {
             Abn = Abn,
             CompanyName = CompanyName
             // ...
             };
          }
public List<LegalEntityDto> GetImportantEntitiesForAccount(int accountNumber){
   var account = DbContext.Account.FirstOrDefault(p => p.accountNumber == accountNumber);
   if (account == null){
      throw AccountNotFoundException("Account not found for accountNumber: " + accountNumber);
   }


   var importantEntities = account.ImportantEntities;
   var dtos = importantEntities.Select(i => i.ToDto()).ToList();
   return dtos;
}
Dto遵循相同的继承结构,即PersonDto&CompanyDto从legalentityto继承

在SQL数据库中,有一个LegalEntity表,它位于每个层次结构实现的表之后,包含每个LegalEntity、Person和Company属性的一列,以及用C模型类名填充的Discriminator列

我有一个方法应该返回一个LegalEntityDto的列表,它可以是Person和/或Company对象的组合。代码类似于下面更多的伪代码:

public abstract class LegalEntity{
// common properties
   public virtual LegalEntityDto ToDto()
   {
      return null;
   }
}

public class Person : LegalEntity
{
   public string FirstName {get; private set;}
   public DateTime? DateOfBirth {get; private set;}
   // ... Other person-specific properties.
   public new PersonDto ToDto()
   {
      return new PersonDto
      {
         {
         Firstname = Firstname,
         DateOfBirth = DateOfBirth
         // ...
         };
      }

public class Company : LegalEntity
    {
       public string Abn {get; private set;}
       public string CompanyName {get; private set;}
       // ... Other company-specific properties.
       public new CompanyDto ToDto()
       {
          return new CompanyDto
          {
             {
             Abn = Abn,
             CompanyName = CompanyName
             // ...
             };
          }
public List<LegalEntityDto> GetImportantEntitiesForAccount(int accountNumber){
   var account = DbContext.Account.FirstOrDefault(p => p.accountNumber == accountNumber);
   if (account == null){
      throw AccountNotFoundException("Account not found for accountNumber: " + accountNumber);
   }


   var importantEntities = account.ImportantEntities;
   var dtos = importantEntities.Select(i => i.ToDto()).ToList();
   return dtos;
}
我的问题是对每个“重要性”调用ToDto,调用抽象LegalEntity ToDto方法,该方法返回null。在运行时,当检查重要度集合时,我可以看到对象包含个人或公司的属性,并显示为Castle.proxy.PersonProxy或Castle.proxy.CompanyProxy类型


我的问题是,我如何访问运行时明显可用的Person或Company属性,或者以某种方式调用ToDto的Person或Company实现?

解决方案,如所述,是在Person和Company实现中抽象和重写基本ToDto方法。

在派生类中,将基对象重写为方法,而不是对其进行阴影处理。这可以通过使基本方法抽象来实现。这就是解决方案,谢谢!