Table Per Type Fluent Mapping in Entity Framework Has Error when map

14 Views Asked by At

Unable to create a 'DbContext' of type ''. The exception ''CorporateStaff' is mapped to the table 'dbo.CorporateStaff' while 'Person' is mapped to the table 'Person'. Map all the entity types in the hierarchy to the same table, or remove the discriminator and map them all to different tables. See https://go.microsoft.com/fwlink/?linkid=2130430 for more information.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728.

public class CorporateStaffConfiguration : IEntityTypeConfiguration<CorporateStaff> 
{
    public void Configure(EntityTypeBuilder<CorporateStaff> builder)
    {
        builder.ToTable("CorporateStaff", SchemaConstant.Base);        
    }
}


public class PersonConfiguration : IEntityTypeConfiguration<Person> 
{
    public void Configure(EntityTypeBuilder<Person> builder)
    {
        builder.ToTable("Person", SchemaConstant.Base);

        builder.HasDiscriminator<int>("PersonType")
            .HasValue<Person>(0)
            .HasValue<CorporateStaff>(1);


        builder.HasOne(x => x.FollowerUser)
            .WithMany(c => c.FollowerPeople)
            .HasForeignKey(fk => fk.FollowerUserId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(x => x.IntroducerUser)
            .WithMany(x => x.IntroducerPeople)
            .HasForeignKey(fk => fk.IntroducerUserId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasMany(m => m.PersonFamilyRelations)
            .WithOne(c => c.FamilyRelationPerson)
            .HasForeignKey(fk => fk.FamilyRelationPersonId);

        builder.HasIndex(c => c.NationalCode).IsUnique();

        builder.HasOne(u => u.User).WithOne(c => c.Person);

        builder.Navigation(u => u.User).AutoInclude();
    }
}
0

There are 0 best solutions below