arame3333
arame3333

Reputation: 10193

Entity Framework TPH Inheritance errors:

I get the error message; error 3032: Problem in mapping fragments starting at line 77:Condition member 'Company.CompanyTypeId' with a condition other than 'IsNull=False' is mapped. Either remove the condition on Company.CompanyTypeId or remove it from the mapping.

I have done a search on how to fix, this but I do not understand the answers.

My classes are

public abstract class Company
    {
        public Company()
        {
            this.AddressLines = new List<AddressLine>();
        }

        public int CompanyId { get; set; }
        public int CompanyTypeId { get; set; }

        public string Comments { get; set; }

        public DateTime? EndOfBusinessDate { get; set; }


        public virtual CompanyType CompanyType { get; set; }
    }

    public class Subcontractor : Company
    {
        public Subcontractor()
        {
            this.SubcontractorTrades = new List<SubcontractorTrade>();
        }

        public virtual ICollection<SubcontractorTrade> SubcontractorTrades { get; set; }
        public string ValueOfWork { get; set; }
        public string QualityAssured { get; set; }
        public int? NumberOfOperatives { get; set; }


        public static IPagedList<Subcontractor> GetSubcontractors(int page, int PageSize)
        {
            using (var db = new SherryGreenGroupContext())
            {
                return db.Subcontractors
                    .Include("SubcontractorTrades")
                    .Include("AddressLines")
                    .Where(x => x.EndOfBusinessDate == null)
                    .OrderBy(x => x.Company1)
                    .ToPagedList(page, PageSize);
            }
        }
    }

My mappings look like;

   public CompanyMap()
    {
        // Primary Key
        this.HasKey(t => t.CompanyId);

        // Properties
        this.Property(t => t.Comments).HasMaxLength(1023);

        // Table & Column Mappings
        this.ToTable("Company");
        this.Property(t => t.CompanyId).HasColumnName("CompanyId");

        this.Property(t => t.CompanyTypeId).HasColumnName("CompanyTypeId");
        this.Property(t => t.Comments).HasColumnName("Comments");
        this.Property(t => t.Created).HasColumnName("Created");

        // Relationships
        this.HasRequired(t => t.CompanyType).WithMany(t => t.Companies).HasForeignKey(d => d.CompanyTypeId);
    }
}

public class SubcontractorMap : EntityTypeConfiguration<Subcontractor>
{
    public SubcontractorMap()
    {
        this.Property(t => t.QualityAssured).IsFixedLength().HasMaxLength(1);
        this.Property(t => t.ValueOfWork).HasMaxLength(255);

        this.Property(t => t.QualityAssured).HasColumnName("QualityAssured");
        this.Property(t => t.ValueOfWork).HasColumnName("ValueOfWork");
        this.Property(t => t.NumberOfOperatives).HasColumnName("NumberOfOperatives");
    }
}

The context class looks like

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CompanyMap());
        modelBuilder.Configurations.Add(new SubcontractorMap());
        modelBuilder.Configurations.Add(new SupplierMap());
        modelBuilder.Configurations.Add(new ArchitectsAndSurveyorMap());
        modelBuilder.Configurations.Add(new StructuralEngineerMap());
        modelBuilder.Configurations.Add(new CostConsultantMap());
        modelBuilder.Configurations.Add(new ServiceEngineerMap());
        modelBuilder.Configurations.Add(new CompanyTypeMap());
                    modelBuilder.Entity<Company>()
            .Map<Subcontractor>(m => m.Requires("CompanyTypeId").HasValue(4))
            .Map<Supplier>(m => m.Requires("CompanyTypeId").HasValue(5))
            .Map<ArchitectsAndSurveyor>(m => m.Requires("CompanyTypeId").HasValue(1))
            .Map<StructuralEngineer>(m => m.Requires("CompanyTypeId").HasValue(2))
            .Map<CostConsultant>(m => m.Requires("CompanyTypeId").HasValue(3))
            .Map<ServiceEngineer>(m => m.Requires("CompanyTypeId").HasValue(6));
    }
}

I have tried commentinout out CompanyTypeId from both the class definition and the mapping code, but I get the same error

Upvotes: 3

Views: 2313

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

If you want to use CompanyTypeId as discriminator for TPH you must not use it for anything else => you cannot have it as property in Company class and you cannot have relation with CompanyType class. The reason is that entity type in inheritance is immutable. Changing either CompanyTypeId or CompanyType in Company class would change the type of the entity but you cannot change the type of existing instance.

Upvotes: 8

Related Questions