【问题标题】:Entity Framework 4.3: Multiplicity constraint violated实体框架 4.3:违反多重性约束
【发布时间】:2012-09-20 22:19:35
【问题描述】:

我有 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 对象时,我收到以下错误:

违反了多重性约束。关系Charges.Infrastructure.DataAccess.FlightCostInfo_Costs 的角色FlightCostInfo_Costs_Source 具有多重性1 或0..1

我不知道为什么会这样。有人可以帮忙吗?

更新: 保存 FlightCostInfo 列表的代码:

        public virtual void Save(IEnumerable<TObject> entities)
        {
            Context.Configuration.AutoDetectChangesEnabled = false;
            entities.ToList().ForEach(entity => 
            {
                if (Equals(entity.Id, default(TKey)) || !Context.ChangeTracker.Entries<TObject>().ToList().Any(dbEntry => dbEntry.Entity.Id.Equals(entity.Id)))
                {
                    Set.Add(entity);
                }
            });

            Context.ChangeTracker.DetectChanges();
            SaveChanges();
            Context.Configuration.AutoDetectChangesEnabled = true;
        }

        protected void SaveChanges()
        {
            var entriesWithGuidKey = Context.ChangeTracker.Entries<BaseEntity<Guid>>().Where(e => e.Entity.Id == Guid.Empty).ToList();
            entriesWithGuidKey.ForEach(e => e.Entity.Id = Guid.NewGuid());

            var entriesWithPeriodicValidity = Context.ChangeTracker.Entries<IPeriodicValidityObject>().ToList();
            entriesWithPeriodicValidity.ForEach(e => 
                {
                   if (e.State != System.Data.EntityState.Unchanged)
                   {
                       e.Entity.ChangedDate = DateTime.UtcNow;
                   }
                });
            Context.SaveChanges();
        }

【问题讨论】:

  • 你能发布你用来创建和保存FlightCostInfo列表的代码吗?
  • @greg84 请看我的更新。
  • 抱歉,我的意思是创建FlightCostInfo 列表并将Cost 实体添加到其中的代码...
  • ...您还可以将 attrbitue [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 添加到您的 Guid 身份列中,以消除对 entriesWithGuidKey 部分的需要。
  • @greg84 我有简单的 POCO。这就是为什么我不对它们使用属性。由于我使用将成本添加到 FlightCostInfo 的 POCO 代码非常简单: flightCostInfo.Costs.Add(new Cost {...})

标签: c# entity-framework-4.1


【解决方案1】:

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

结束问题

【讨论】:

    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多