I work with EF6 by first switching from edmx to Code. I did the reverse engineering from an existing DB, and I am tring to reproduce the model I had before, with a nested TPH made in a table called "Contacts", which has two different fields "Type" and "Subtype" respectively as the first , and the first discriminator of the second level.
I would like to have something like this:
Contact
Model (Type = 1)
BookingModel (SubType = 1)
ScoutingModel (Subtype = 2)
Customer (Type = 2)
Agency (SubType = 3)
Client (Subtype = 4)
Service (Subtype = 5)
I encoded the Contact, Model, and Customer classes as Abstract and BookingModel, ScoutingModel, Agency, Client, Service as Concrete without including the Type and Subtype fields, as they are discriminators.
Here is the display code:
public virtual DbSet<Contact> Contacts { get; set; }
modelBuilder.Entity<Contact>()
.Map<Model>(e => e.Requires("Type").HasValue((byte)ContactTypeEnum.Model))
.Map<Customer>(e => e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer));
modelBuilder.Entity<Customer>()
.Map<Agency>(e => {
e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer);
e.Requires("SubType").HasValue((byte)CustomerTypeEnum.Agency);
})
.Map<Client>(e => {
e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer);
e.Requires("SubType").HasValue((byte)CustomerTypeEnum.Client);
})
.Map<Service>(e => {
e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer);
e.Requires("SubType").HasValue((byte)CustomerTypeEnum.Service);
});
modelBuilder.Entity<Model>()
.Map<BookingModel>(e => {
e.Requires("Type").HasValue((byte)ContactTypeEnum.Model);
e.Requires("SubType").HasValue((byte)ModelTypeEnum.Booking);
})
.Map<ScoutingModel>(e => {
e.Requires("Type").HasValue((byte)ContactTypeEnum.Model);
e.Requires("SubType").HasValue((byte)ModelTypeEnum.Scouting);
});
But this does not work, I get an error, for example
(52,10): 3023: , 52, 68, 75, 82, 89, 98, 106, 729, 747: Contact.Type NULL. .
"" "" Ninyable Tinyint . NULL , .
- , ?