I have 2 classes:
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; } } And mapping for them:
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); } } When I'm saving List of FlightCostInfo where each contains one or more Cost objects I recieve following error:
Multiplicity constraint violated. The role FlightCostInfo_Costs_Source of the relationship Charges.Infrastructure.DataAccess.FlightCostInfo_Costs has multiplicity 1 or 0..1
I don't have any idea why this happens. Could anyone help?
Update: Code to save list of 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?FlightCostInfoand adds theCostentities to them...[DatabaseGenerated(DatabaseGeneratedOption.Identity)]to your Guid identity columns to remove the need for theentriesWithGuidKeysection.