我有2个班级:
public class FlightCostInfo : BaseEntity<Guid>
{
public Flight FlightInfo { get; set; }
public virtual ICollection<Cost> Costs { get; set; }
public virtual ICollection<PriceCalculationNotification> Notifications { get; set; }
public Guid CalculationResultId { get; set; }
}
public class Cost : BaseEntity<Guid>
{
public BasePrice Price { get; set; }
public decimal Amount { get; set; }
public Vendor Vendor { get; set; }
public Guid FlightCostInfoId { get; set; }
}
为他们绘制地图:
internal class FlightCostInfoMapping : EntityTypeConfiguration<FlightCostInfo>
{
public FlightCostInfoMapping()
{
HasKey(i => i.Id);
Property(i => i.CalculationResultId).HasColumnName("CalculationResult_Id");
HasOptional(i => i.FlightInfo);
HasMany(i => i.Costs).WithRequired().HasForeignKey(c => c.FlightCostInfoId);
HasMany(i => i.Notifications).WithRequired().HasForeignKey(n => n.FlightCostInfoId);
}
}
internal class CostMapping : EntityTypeConfiguration<Cost>
{
public CostMapping()
{
HasKey(c => c.Id);
Property(c => c.FlightCostInfoId).HasColumnName("FlightCostInfo_Id");
HasRequired(c => c.Price);
HasRequired(c => c.Vendor);
}
}
当我保存 FlightCostInfo列表,其中每个包含一个或多个Cost对象时,我收到以下错误:
违反了多重性约束。关系FlightCostInfo_Costs_Source
的{{1}}角色具有多重性1或0..1
我不知道为什么会这样。有人可以帮忙吗?
更新: 保存FlightCostInfo列表的代码:
Charges.Infrastructure.DataAccess.FlightCostInfo_Costs
答案 0 :(得分:2)
问题似乎出现在BaseEntity的Equals重载中。所以EF认为FlightCostInfo集合中的所有Cost对象都是相同的。
结束问题