【发布时间】:2023-01-11 05:14:15
【问题描述】:
我有一个用数据库优先方法编写的程序;我有一个表 ServicePlan 和另一个 ServicePlanDetails。它们没有相互映射,但是它们有一个共同的列PlanId;一个servicePlan可以包含多个ServicePlanDetails,就像它的列表一样。
我不想对数据库进行任何更改,但我也想映射它们。我怎样才能做到这一点?在创建模型的方法中执行此操作是否会为我完成工作并且不会更改数据库中的任何内容?我试过这个但可以得到结果。
为简单起见,我只添加了几列及其映射,而不是全部:
public partial class ServicePlan
{
public ServicePlan()
{
ServicePlanDetails = new HashSet<ServicePlanDetail>();
}
public long PlanId { get; set; }
public decimal PhoneId { get; set; }
public byte? NLines { get; set; }
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
public virtual ICollection<ServicePlanDetail> ServicePlanDetails { get; set; }
}
public partial class ServicePlanDetail
{
public long PlanId { get; set; }
public string? ServCode { get; set; }
public string? CountryCode { get; set; }
public bool? IsPlan { get; set; }
public decimal? Cost { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ServicePlan>(entity =>
{
entity.HasKey(e => e.PlanId).HasName("PK_UsersPlan");
entity.ToTable("ServicePlan");
entity.HasIndex(e => e.VideoTronId, "IDX_VTID").HasFillFactor(80);
entity.HasIndex(e => new { e.PhoneId, e.IsApproved }, "Ix_SrvcPlan").HasFillFactor(80);
entity.Property(e => e.Zone).HasMaxLength(50);
entity.HasMany(p => p.ServicePlanDetails)
.WithOne()
.HasPrincipalKey(p => p.PlanId)
.HasForeignKey(p => p.PlanId);
});
}
我得到的错误是:
无法确定类型为“ICollection”的导航“ServicePlan.ServicePlanDetails”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。
我想将与
serviceplan具有相同planid的serviceplandetails放入serviceplan的列表中。
【问题讨论】:
-
试用 EF Core 电动工具