0

I'm quite new to .Net Core & Entity Framework. I'm working on a .Net Core project that has a database that was created using the Code First Approach. The following inheritance structure exists in the model (pseudo-code):

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
             // ...
             };
          }

(the Dto's follow the same inheritance structure i.e PersonDto & CompanyDto inherit from LegalEntityDto).

In the SQL database, there is a LegalEntity table which following the Table Per Hierarchy implementation contains a column for each LegalEntity, Person and Company property, plus the Discriminator column (which is populated with the C# model class name).

I have a method that is supposed to return a list of LegalEntityDto's, which could be a combination of Person and/or Company object. Code is similar to the below (more pseudo-code):

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;
}

My problem is on the ToDto() call for each 'importantEntity', the abstract LegalEntity ToDto() method is called (which returns null). At run-time I can see when inspecting the collection of importantEntities that the objects contain the properties of either Person or Company, and are shown as a Castle.Proxies.PersonProxy or Castle.Proxies.CompanyProxytype.

My question is, how can I access the Person or Company properties that are obviously available at runtime, or somehow invoke the Person or Company implementation of ToDto() ?

2
  • In derived classes, override base ToDto method rather than shadowing it. This can be enforced by making base method abstract. Commented Mar 6, 2019 at 8:05
  • This was the solution, thanks! Commented Mar 6, 2019 at 23:31

1 Answer 1

1

Solution, as commented by Ivan Stoev, was to make the base ToDto() method abstract and override in the Person and Company implementations.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.