实体框架4.3:违反了多重性约束

时间:2012-09-20 09:13:41

标签: c# entity-framework-4.1

我有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

1 个答案:

答案 0 :(得分:2)

问题似乎出现在BaseEntity的Equals重载中。所以EF认为FlightCostInfo集合中的所有Cost对象都是相同的。

结束问题