gpt4 book ai didi

entity-framework - Entity Framework TPH 继承错误 :

转载 作者:行者123 更新时间:2023-12-03 07:11:28 27 4
gpt4 key购买 nike

我收到错误消息;错误 3032:从第 77 行开始映射片段时出现问题:映射条件成员“Company.CompanyTypeId”,其条件不是“IsNull=False”。删除 Company.CompanyTypeId 上的条件或将其从映射中删除。

我已经搜索了如何解决这个问题,但我不明白答案。

我的类(class)是

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,但出现相同的错误

最佳答案

如果您想使用 CompanyTypeId 作为 TPH 的鉴别器,则不得将其用于其他任何用途 => 您不能将其作为 Company 类中的属性,并且不能与 CompanyType< 建立关系 类。原因是继承中的实体类型是不可变的。更改 Company 类中的 CompanyTypeIdCompanyType 将会更改实体的类型,但您无法更改现有实例的类型。

关于entity-framework - Entity Framework TPH 继承错误 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13699117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com