我收到错误消息; 错误3032:从第77行开始映射片段时出现问题:映射条件成员'Company.CompanyTypeId',条件不是'IsNull = False'。删除Company.CompanyTypeId上的条件或从映射中删除它。
我已经完成了如何修复的搜索,但是我不明白答案。
我的课程
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);
}
}
}
我的映射看起来像;
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");
}
}
上下文类看起来像
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));
}
}
我已尝试从类定义和映射代码中注释出CompanyTypeId,但我得到了相同的错误
答案 0 :(得分:6)
如果您想使用CompanyTypeId作为TPH的鉴别器,则不得将其用于其他任何内容=&gt;你不能把它作为Company
类中的属性,你不能与CompanyType
类有关系。原因是继承中的实体类型是不可变的。更改CompanyTypeId
类中的CompanyType
或Company
将更改实体的类型,但您无法更改现有实例的类型。