1

I currently have the following classes in my EF based project. Data is stored in a SQL Database using the Table per Hierarchy (TPH) configuration :

class Lot {
    Building building
    ...
}


class Building {
     string name {get; set;}
     string address {get; set;}
     string discriminator {get; set;}
     ...
}

class House : Building {
    ...
}


class Employee: {
     Building building {get; set;}
     Person person {get; set;}
     string jobName {get; set;}
}

class Shop: Building {
     List<Employee> employees {get; set;}
     Person owner {get; set;}
     ...
}

class Factory: Building {
     List<Employee> employees {get; set;}
     List<Machine> equipment {get; set;}
     ...
}

I setup my ef model this way using Ardalis clean architecture configuration classes:

...
builder.HasMany(x => ((Shop)x).employees).WithOne(x => x.building);
builder.HasDiscriminator(x => x.discriminator)
  .HasValue<House>("House")
  .HasValue<Shop>("Shop")
  .HasValue<Factory>("Factory");

Finally in my lot specification where I need to access all the informations I include all the stuff I should need to access

Query.Include(x => x.building).ThenInclude(x => (x as Shop).employees).ThenInclude(x => x.person)

My relationships between the Lot and Building works properly, but the one between Shop and Employees doesn't. Would you have any idea how to make it work? I need an employee list in both Shop and Factory and both are too different to be in a same class.

I tried to make a ModelBuilder for the Shop class specifically that would define the relationship between Shop but it also didn't worked properly. I also try to swap between different type of cast for Building to Shop.

  • If this is still a question please rephrase so that the question part is Clear. it's a skill to ask a good question on stack overflow that people can answer. my advice is to completely separate your question from the rest of the details of your situation. I'm following this question and when it's rephrased I will do my best to answer. – Luke Hammer Feb 13 '23 at 23:09

0 Answers0