How-to delete a many-to-many relationship

May 14, 2014 at 6:40 PM
Hi

I have started to use this framework in a new project, but now we need to delete a many-to-many relationship, our tries are something like this:

entityA.EntitiesBCollection.Remove(entityB);

unitOfWork.SaveChanges();


But a InvalidOperationException is thrown with this message: "The ObjectStateEntry is a relationship entry. The current and original values of relationship entries cannot be modified."


How can i delete a many-to-many relationship between to entities?


Thanks

Desbaratizador
Coordinator
May 15, 2014 at 6:13 AM
This shouldn't be any different than how you would delete this using EF directly. Please be sure to set the ObjectState of your entity to ObjectState.Deleted.
Marked as answer by lelong37 on 5/14/2014 at 10:13 PM
May 19, 2014 at 11:06 PM
This stacktrace is useful?

at System.Data.Entity.Core.Objects.RelationshipEntry.GetUpdatableOriginalValues()
at EFHooks.IaExtensions.<GetDeletedRelationships>b__2(ObjectStateEntry e, Int32 i)
at EFHooks.IaExtensions.<>c__DisplayClass7.<GetRelationships>b__5(ObjectStateEntry e)
at System.Linq.Enumerable.<>c__DisplayClass123.<CombineSelectors>b__11(TSource x)
at System.Linq.Enumerable.WhereSelectArrayIterator
2.MoveNext()
at System.Collections.Generic.List1.InsertRange(Int32 index, IEnumerable1 collection)
at System.Collections.Generic.List1.AddRange(IEnumerable1 collection)
at EFHooks.HookedDbContext.SaveChanges()
at Repository.Pattern.Ef6.DataContext.SaveChanges()