views:

55

answers:

0

I have model KlientTrasa which has reference to objects Klienci and Trasy.

I'm trying to delete all references and then add new ones in this way:

        // delete all
        foreach(var kt in trasa.KlientTrasa.ToList())
        {
            //_entities.DeleteObject(kt);
            trasa.KlientTrasa.Remove(kt);
        }

        // add all
        int count = 0;
        foreach (int i in KlienciID)
        {
            var ktNew = new KlientTrasa { Trasy = trasa };
            ktNew.KlienciReference.EntityKey = ktNew.KlienciReference.NewKey(i);
            ktNew.Seq = count++;
            trasa.KlientTrasa.Add(ktNew);                     
        }

But I get error:

A relationship is being added or deleted from an AssociationSet 'FK_KlientDoTrasa_Trasy'. With cardinality constraints, a corresponding 'KlientTrasa' must also be added or deleted.

My definition or relations is:

[assembly: global::System.Data.Objects.DataClasses.EdmRelationshipAttribute("bottlerModel", "FK_Trasy_Kierowcy", "Kierowcy", global::System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(MVC_Test1.Models.Kierowca), "Trasy", global::System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(MVC_Test1.Models.Trasa))]
[assembly: global::System.Data.Objects.DataClasses.EdmRelationshipAttribute("bottlerModel", "FK_KlientDoTrasa_Klienci", "Klienci", global::System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(MVC_Test1.Models.Klient), "KlientTrasa", global::System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(MVC_Test1.Models.KlientTrasa))]
[assembly: global::System.Data.Objects.DataClasses.EdmRelationshipAttribute("bottlerModel", "FK_KlientDoTrasa_Trasy", "Trasy", global::System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(MVC_Test1.Models.Trasa), "KlientTrasa", global::System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(MVC_Test1.Models.KlientTrasa))]

However, if I do _entites.SaveChanges() twice, one time after deleting and one time after adding then it works, but I would prefer to have one SaveChanges only.