Search code examples
sqlsql-serverentity-frameworkef-code-firstdiscriminator

Not set "Discriminator" column when save entity in Entity Framework


I have a base class Person:

[KnownType(typeof(Doctor))]
public abstract class Person
{
    public int PersonId { get; set; }

    public string FirstName { get; set; }

    public string MiddleName { get; set; }

    public string LastName { get; set; }

    public string Gender { get; set; }

    public DateTime BirthDate { get; set; }

    public string Email { get; set; }

    public string MobilePhoneNumber { get; set; }

    public string HomePhoneNumber { get; set; }

    public bool IsGlobal { get; set; }

    public bool IsDeleted { get; set; }

    public bool IsApproved { get; set; }

    public int? FacilityId { get; set; }

    public int? AddressId { get; set; }

    public virtual FacilityView Facility { get; set; }

    public virtual Address Address { get; set; }

    public virtual ICollection<Organization> Organizations { get; set; }

    public virtual ICollection<TenantEntity> TenantEntities { get; set; }
}

And have nested class Doctor:

public class Doctor : Person
{
    public string Speciality { get; set; }
}

Table Person script

CREATE TABLE [core].[Person](
[PersonId] [int] IDENTITY(1,1) NOT NULL,
[Discriminator] [nvarchar](255) NOT NULL,
[FirstName] [nvarchar](255) NOT NULL,
[MiddleName] [nvarchar](255) NULL,
[LastName] [nvarchar](20) NOT NULL,
[Gender] [nvarchar](20) NOT NULL,
[BirthDate] [date] NOT NULL,
[Email] [nvarchar](250) NULL,
[MobilePhoneNumber] [nvarchar](250) NULL,
[HomePhoneNumber] [nvarchar](250) NULL,
[IsGlobal] [bit] NOT NULL,
[IsDeleted] [bit] NOT NULL,
[IsApproved] [bit] NOT NULL,
[FacilityId] [int] NULL,
[AddressId] [int] NULL,
[Speciality] [nvarchar](250) NULL,

And when i try to save new Doctor entity i have an error:

Cannot insert the value NULL into column 'Discriminator'

What i do wrong in this situation?Why EF not save "Doctor" value in Discriminator field?

UPDATE:

part from DBContext:

public DbSet<Person> Persons { get; set; }
#region Person

        modelBuilder.Entity<Person>()
            .ToTable("Person", "core")
            .HasKey(t => t.PersonId);

        modelBuilder.Entity<Person>()
            .HasOptional(t => t.Facility);

        modelBuilder.Entity<Person>()
            .HasOptional(t => t.Address);

        modelBuilder.Entity<Person>()
            .HasMany(x => x.Organizations)
            .WithMany()
            .Map(x =>
            {
                x.MapLeftKey("PersonId");
                x.MapRightKey("OrganizationId");
                x.ToTable("PersonOrganization", "core");
            });

        modelBuilder.Entity<Person>()
            .HasMany(x => x.TenantEntities)
            .WithMany()
            .Map(x =>
            {
                x.MapLeftKey("PersonId");
                x.MapRightKey("TenantEntityId");
                x.ToTable("PersonTenantEntity", "core");
            });

        #endregion

Solution

  • The default inheritance mapping does not define a discriminator column if only one derived class exists. Your code will work if you define a second class that derives from Person:

    public class Nurse : Person
    { 
        public string Whatever {get;set;}
    }
    

    Or add the discriminator explicitly to your model.

    modelBuilder.Entity<Doctor>().Map(p => p.Requires("Discriminator").HasValue("Doctor"));