4

I'm working with EF6, migrating from edmx to Code first. I did a reverse engineering from an existing DB, and I'm tring to reproduce the model I had before with a nested TPH done on a table called "Contacts" wich has 2 different fields "Type" and "SubType" respectively as first and second level discriminator.

I'd 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've coded Contact, Model and Customer classes as Abstract and BookingModel, ScoutingModel, Agency, Client, Service as Concrete without including "Type" and "Subtype" fields as they are the discriminators.

Here is the mapping 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 is not working, I'm geting an error like

(52,10) : error 3023: Problem in mapping fragments starting at lines 52, 68, 75, 82, 89, 98, 106, 729, 747:Column Contact.Type has no default value and is not nullable. A column value is required to store entity data.

The "Type" and "SubType" fields in the DB are Tinyint nullable with no default values. I've tried also to change them as nullable or giving a default value but it doesn't change the resulting error.

Am I doing something wrong in setting the mapping, or am I choosing a wrong way to face this scenario?

Johan
  • 262
  • 1
  • 15
ilFusta
  • 103
  • 9
  • Hi, did you manage to solve this issue? I am facing the same problem. I tried several solutions without any luck. It seems to me, that code first approach is lacking the opportunity to set default values at model level. – Daniel Leiszen Nov 17 '15 at 19:56

0 Answers0