Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.1 Codefirst: "Given multiplicity constraints" error when deleting one-to-many children

I have the following classes in Entity Framework 4.1 (the classes have been pruned to keep the code readable)

public class MetaInformation
{
    public int Id { get; set; }
    public virtual MetaInformationObject RelatedTo { get; set; }
}

public abstract class MetaInformationObject
{
    public int Id { get; set; }
    public virtual List<MetaInformation> MetaInformations { get; set; }
}

[Table("Hardwares")]
public class Hardware : MetaInformationObject
{
    [MaxLength(100)]
    public string Name { get; set; }
}

public class Inventory
{
    public int Id { get; set; }
    public virtual List<Machine> Machines { get; set; }
}

public class Machine : Hardware
{
    public Inventory Inventory { get; set; }
}

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Isis.Business.Base.MetaInformationObject>()
        .HasMany<Isis.Business.Base.MetaInformation>(mi => mi.MetaInformations).WithRequired(mi => mi.RelatedTo).WillCascadeOnDelete(true);
    modelBuilder.Entity<Isis.Business.Inventory.Inventory>()
            .HasMany<Isis.Business.Inventory.Machine>(i => i.Machines).WithRequired(m => m.Inventory).WillCascadeOnDelete(true);

    base.OnModelCreating(modelBuilder);
}

At one point, i'd like to update a machine information, so if it already exists in the database, i load it, attach it and then clear the previous metainformations to replace them with the new ones.

public void UpdateMachine(Inventory i, Machine m)
{
    DataContext.Dao.Db.Inventories.Attach(i);
    if (!i.Machines.Exists(InnerHardware => InnerHardware.SerialNumber == m.SerialNumber)) {
        i.Machines.Add(m);
    } else {
        var workingMachine = i.Machines.First(Machine => Machine.SerialNumber == m.SerialNumber);
        Dao.Db.Machines.Attach(workingMachine);
        if (workingMachine.MetaInformations != null && workingMachine.MetaInformations.Count > 0) {
            workingMachine.MetaInformations.Clear();
            //workingMachine.MetaInformations.ForEach(mi => { Dao.Db.MetaInformations.Attach(mi); Dao.Db.MetaInformations.Remove(mi); }); // tried this to, with variations
        }
        workingMachine.MetaInformations = m.MetaInformations;
    }
    DataContext.Dao.Db.SaveChanges();
}

And then, the following DbUpdateException is thrown:

A relationship from the 'MetaInformationObject_MetaInformations' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'MetaInformationObject_MetaInformations_Target' must also in the 'Deleted' state.

I tried following some questions here on SO to solve the problem, especially trying to read this one and the link provided in the answer (that's the reason why there's a direct reference from MetaInformation to MetaInformationObject) but i can't figure out what's wrong.

like image 288
samy Avatar asked Jul 29 '11 07:07

samy


1 Answers

Cascade delete will not help you here. Cascade delete works only if you delete parent entity (MetaInformationObject). The problem here is that calling Clear on the collection of related objects doesn't delete them. It only deletes the relation (= it will set foreign key in MetaInformation to null) but it is not allowed by your mapping constraints (you defined relation as required). The ways to avoid this are either:

  • Iterate through all related MetaInformations and delete each of them
  • Refactor you model to define identifying relation. After that Clear will not only break the relation but mark related entity as deleted as well.
like image 82
Ladislav Mrnka Avatar answered Oct 18 '22 12:10

Ladislav Mrnka