1

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(); } 
4
  • Can you post the code you're using to create and save the list of FlightCostInfo? Commented Sep 20, 2012 at 9:41
  • Sorry, I meant the code that creates the list of FlightCostInfo and adds the Cost entities to them... Commented Sep 20, 2012 at 12:04
  • ...You could also add the attrbitue [DatabaseGenerated(DatabaseGeneratedOption.Identity)] to your Guid identity columns to remove the need for the entriesWithGuidKey section. Commented Sep 20, 2012 at 12:05
  • @greg84 I Have Simple POCOs. That is why i don't use attributes on them. As I use POCOs code which adds Cost to FlightCostInfo is very simple: flightCostInfo.Costs.Add(new Cost {...}) Commented Sep 20, 2012 at 12:36

1 Answer 1

2

The problem appeared to be in Equals overload for BaseEntity. So EF thought that all Cost objects in FlightCostInfo collection are equal.

Closing the question

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.