3

我收到错误消息;错误 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,但我得到了同样的错误

4

1 回答 1

7

如果你想使用 CompanyTypeId 作为 TPH 的鉴别器,你不能将它用于其他任何东西 => 你不能将它作为Company类中的属性,你不能与CompanyType类有关系。原因是继承中的实体类型是不可变的。更改其中一个CompanyTypeIdCompanyTypeCompany类中会更改实体的类型,但您不能更改现有实例的类型。

于 2012-12-04T10:38:54.057 回答