I'm getting the error when attempting to add to a client-side EntitySet. The following code intermittently fails on the Add
method.
// On the DB side this has a primary key field of IDENTITY(1,1) called Id
var map = new EpisodeDictionaryMap();
map.DictionaryName = dictionary; // String
map.Section = section; // String
map.DictionaryVersion = version; // Int32
episode.EpisodeDictionaryMap.Add(map);
I'm also calling Clear()
on the EntitySets for Episode
and EpisodeDictionaryMap
(amongst others) not before every call to this code, but every now and again, when the user changes to a different screen within the app. This is a recent change, so could be part of the problem.
Upon investigating the DomainContext's EpisodeDictionaryMap collection it would appear that sometimes there are in fact multiple 'New' EpisodeDictionaryMap instances, each with an Id of 0
, as I would expect prior to saving, and in these instances the save completes without issue, and the correct Ids are assigned to the client-side entities.
Given that it doesn't seem to be an issue to have multiple entries with an Id of 0
in the EntitySet at any one time (at least whilst they're in a "New" state), I don't understand what the error message is trying to tell me.
I've noticed on some related SO questions that Composition can be an issue, however I'm not using it. With the exception of the Episode reference itself there are also no other foreign key references, which also appears to sometimes cause this error.
Suggestions, thoughts? Many thanks!
Edit:
Full exception (no inner exception exists)
System.InvalidOperationException occurred
Message=An entity with the same identity already exists in this EntitySet.
StackTrace:
at System.ServiceModel.DomainServices.Client.EntitySet.AddInternal(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.<Add>b__5(EntitySet l, Entity e)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.VisitEntityCollection(IEntityCollection entityCollection, PropertyInfo propertyInfo)
at System.ServiceModel.DomainServices.Client.EntityVisitor.Visit(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.VisitEntityRef(IEntityRef entityRef, Entity parent, PropertyInfo propertyInfo)
at System.ServiceModel.DomainServices.Client.EntityVisitor.Visit(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.VisitEntityRef(IEntityRef entityRef, Entity parent, PropertyInfo propertyInfo)
at System.ServiceModel.DomainServices.Client.EntityVisitor.Visit(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Visit(Entity entity)
at System.ServiceModel.DomainServices.Client.EntitySet.AddAttachInferrer.Infer(EntityContainer container, Entity entity, Action`2 action)
at System.ServiceModel.DomainServices.Client.EntitySet.Add(Entity entity)
at System.ServiceModel.DomainServices.Client.EntityCollection`1.Add(TEntity entity)
at TangoSoft.Client.EpisodeExtensions.SaveDictionaryVersion(Episode episode, String dictionary, String section, Int32 version)
SaveChanges()
? I would continue debugging by inspecting individual properties of the exception (perhaps anInnerException
exists) and looking for clues. – RobSaveChanges()
is called only after a manually triggered click event, and has not been triggered at the point of the error (I've verified that). I've included the full exception in the question; unfortunately it's utterly useless. – Wayne Cornish