Entity Framework how to setup relationships with Inheritance types

90 views Asked by At

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.

0

There are 0 answers