An entity with the same identity already exists in this EntitySet - silverlight

I'm trying to peform an update statement using WCF RIA Services, but everytime I update I keep getting "An entity with the same identity already exists in this EntitySet. Any insight on where I can start looking or figuring out what is wrong?
Step 1
LoadOperation<Analysis> AnalysisLP = ((App)Application.Current)._context.
Load(((App)Application.Current)._context.GetAnalysisQuery().
Where(o => o.ProjectID == Convert.ToInt32(((App)Application.Current).Project.ProjectID)));
Step 2
AnalysisLP.Completed += delegate
{
if (!AnalysisLP.HasError)
{
Analysis = AnalysisLP.Entities.FirstOrDefault();
};
Step 3
((App)Application.Current)._context.Analysis.Attach(Analysis);
((App)Application.Current)._context.SubmitChanges(OnSubmitCompleted, null);
Can anyone help me, what is it i'm doing wrong??
thanks

Your object Analysis comes from the EntitySet via a query but is still attached to that EntitySet.
You just need to change its properties and call SubmitChanges. Do not try to attach it again.

To avoid the “An Entity with the same identity already exists in the EntitySet” exception, Entities that are updated, modified or deleted must always be fully refreshed from server upon saving, there can be no references held in memory to the previous instances of the entities. To prevent orhpaned instances from hanging around, I follow these rules:
Entity instances should not have any property changed event handlers assigned directly to them, rather use OnCreated or OnPropertyNameChanged partial methods instead.
When entities are added to an EntitySet, do not assign parent Entity instance references, use the foreign key ID property instead (myEntity.ParentalID = SelectedParent.ParentalID rather than myEntity.Parent = SelectedParent) because the SelectedParent probably isn’t getting reloaded upon saving because it isn’t part of the unit of work, so that reference will be held after the save and refresh.
Any combo boxes that are used as populate sources for Entity properties of the Entity need to have their EntitySet reloaded after saving as well; otherwise those related Entities populating the combo will hold references to the previous entity instance.

Related

BreezeJs Local query returns with entity and empty entity

I'm closely following John Papa's pluralsight course on Angular and Breeze. I also use Entity Framework 6.
At load time, I call a Prime function that clears the cache:
function clearCache() {
var cachedParents = manager.getEntities('Parent'); // all invoices in cache
cachedParents.forEach(function (parent) { manager.detachEntity(parent); });
zStorage.clear();
manager.clear();
}
and then, loads the info:
return EntityQuery.from('Parents')
.where('applicationUser.email', '==', userId)
.expand('Address, Children')
.toType(entityParent)
.using(self.manager)
.execute()
.then(querySucceeded, self._queryFailed);
that calls the controller with
[HttpGet]
public IQueryable<Parent> Parents()
{
return _repository.Parents;
}
That returns one record...
Later, on the loading of the view, in the same repository, I request the parent entity from the local cache as follows:
var cachedParents = EntityQuery.from('Parent')
.toType(entityParent)
.using(self.manager)
.executeLocally();
THIS ONE BRINGS TWO ENTITIES, the correct one with Id, name, addres, etc. but also brings an empty entity with Id 0.
I've checked and even if I call the local query right after the remote query, it still brings the correct record AND the empty record.
I also reviewed the response and the json object comes correctly formatted and with only one record.
I've tried clearing the zstorage, the entity manager, detachment of the object, but nothing seems to explain or clear the empty entity.
This behavior only happens in the Parent entity type. No other type shows anything wrong.
Thanks in advance.
One way to debug this is to subscribe to the entityManager.hasChangesChanged event.
This event will be fired when the "ghost" entity is added. This way, you can trace the call stack by putting a breakpoint inside the event.
So first, ensure that after the clearCache call, the entityManager is empty. (Side note: the call to individually detach the parent entity via manager.detachEntity is actually redundant since you're already calling the manager.clear method at the end)
Then, put a breakpoint inside the hasChangesChanged event as you debug.
Hope this helps.
My bet is that you have code that adds a "nullo" (a placeholder entity with id=0) to the EntityManager. There is such code in John's sample and you might be calling it unintentionally.
To demonstrate that Breeze is NOT adding such a nullo itself,
set a breakpoint before the query
set a breakpoint at the top of the querySucceeded
confirm that there are no entities in cache at all before you query for "Parents" (e.g., manager.getEntities() returns nothing).
confirm at the top of querySucceeded that the query results in exactly ONE entity in cache (e.g., manager.getEntities() returns the lone "Parent" entity).
FWIW, there is no need to detach individual entities of type "Parent" if you are going to call manager.clear(). That call detaches every entity in the manager's cache, including the "Parent" types.

SL RIA app - Insert and Update using standard generated code does not work - is there a better way?

I have a Silverlight RIA app where I share the models and data access between the MVC web app and the Silverlight app using compiler directives, and for the server, to see what context I am running under I would check to see if the ChangeSet object was non-null (meaning I was running under RIA rather than MvC). Everything works alright but I had problems with the default code generated by the domain service methods.
Let's say I had a Person entity, who belonged to certain Groups (Group entity). The Person object has a collection of Groups which I add or remove. After making the changes, the SL app would call the server to persist the changes. What I noticed happening is that the group entity records would be inserted first. That's fine, since I'm modifying an existing person. However, since each Group entity also has a reference to the existing person, calling AddObject would mark the whole graph - including the person I'm trying to modify - as Added. Then, when the Update statement is called, the default generated code would try to Attach the person, which now has a state of Added, to the context, with not-so-hilarious results.
When I make the original call for an entity or set of entities in a query, all of the EntityKeys for the entities are filled in. Once on the client, then EntityKey is filled in for each object. When the entity returns from the client to be updated on the server, the EntityKey is null. I created a new RIA services project and verified that this is the case. I'm running RIA Services SP1 and I am not using composition. I kind of understand the EntityKey problem - the change tracking done is on two separate contexts. EF doesn't know about the change tracking done on the SL side. However, it IS passing back the object graph, including related entities, so using AddObject is a problem unless I check the database for the existence of an object with the same key first.
I have code that works. I don't know how WELL it works but I'm doing some further testing today to see what's going on. Here it is:
/// <summary>
/// Updates an existing object.
/// </summary>
/// <typeparam name="TBusinessObject"></typeparam>
/// <param name="obj"></param>
protected void Update<TBusinessObject>(TBusinessObject obj) where TBusinessObject : EntityObject
{
if (this.ChangeSet != null)
{
ObjectStateManager objectStateManager = ObjectContext.ObjectStateManager;
ObjectSet<TBusinessObject> entitySet = GetEntitySet<TBusinessObject>();
string setName = entitySet.EntitySet.Name;
EntityKey key = ObjectContext.CreateEntityKey(setName, obj);
object dbEntity;
if (ObjectContext.TryGetObjectByKey(key, out dbEntity) && obj.EntityState == System.Data.EntityState.Detached)
{
// An object with the same key exists in the DB, and the entity passed
// is marked as detached.
// Solution: Mark the object as modified, and any child objects need to
// be marked as Unchanged as long as there is no Domainoperation.
ObjectContext.ApplyCurrentValues(setName, obj);
}
else if (dbEntity != null)
{
// In this case, tryGetObjectByKey said it failed, but the resulting object is
// filled in, leading me to believe that it did in fact work.
entitySet.Detach(obj); // Detach the entity
try
{
ObjectContext.ApplyCurrentValues(setName, obj); // Apply the changes to the entity in DB
}
catch (Exception)
{
entitySet.Attach(obj); // Re-attach the entity
ObjectContext.ApplyCurrentValues(setName, obj); // Apply the changes to the entity in DB'
}
}
else
{
// Add it..? Update must have been called mistakenly.
entitySet.AddObject(obj);
}
}
else
DirectInsertUpdate<TBusinessObject>(obj);
}
Quick walkthrough: If the ChangeSet is null, I'm not under the RIA context, and therefore can call a different method to handle the insert/update and save immediately. That works fine as far as I can tell. For RIA, I generate a key, and see if it exists in the database. If it does and the object I am working with is detached, I apply those values; otherwise, I force detach and apply the values, which works around the added state from any previous Insert calls.
Is there a better way of doing this? I feel like I'm doing way too much work here.
In this kind of a case, where you're adding Group entities to Person.Groups, I would think of just saving the Person and expect RIA to handle the Groups for me.
But let's take a step back, how are you trying to persist your changes? You shouldn't be saving/updating entities one by one. All you have to do is call DomainContext.SubmitChanges and all your changes should be persisted.
I work with pretty complicated projects and I seldom ever have to touch add/update code.
This question has been around with no solid answer, so I'll tell you what I did... which is nothing. That's how I handled it in RIA services, using the code above, since I was sharing the RIA client model and the server model.
After working with RIA services for a year and a half, I'm in the camp that believes that RIA services is good for working with smaller, less complex apps. If you can use [Composite] for your entities, which I couldn't for many of my entities, then you're fine.
RIA services can make throwing together small applications where you want to use the entity from EF really quick, but if you want to use POCOs or you foresee your application getting complex in the future, I would stick with building POCOs on the service end and passing those through regular WCF, and using shared behaviors by making your POCOs partial classes and sharing the behavior code with the client. When you're trying to create models that work the same on the client and the server, I had to write a ridiculous amount of plumbing code to make it work.
It definitely IS possible to do, I've done it; but there is a lot of hoops you must jump through for everything to work well, and I never fully took into consideration things like your shared model pre-loading lists for use on the client, whereas the server didn't need these preloaded everytime and actually slowed down the loading of the web page unnecessarily and countering by writing hacky method calls which I had to adopt on the client. (Sorry for the run-on.) The technique I chose to use definitely had its issues.

RIA: Loading entity sets

I am missing something very fundamental when working with SL4 RIA entities.
I have a Domain Service with User entities. On the service context, I have a method:
EntityQuery<User> GetUsersQuery()
I perform a load like so:
context.Load(context.GetUsersQuery(), (loadOp)=>
{
// Things done when the load is completed
}, null);
When the Completed action executes, the loadOp.Entities collection is full of the User entities, but they are not attached to the context.Users entity set. It also appears that I can't attach them manually from the callback. What important step am I missing to get these tracked in the entity set?
Just to elaborate, in the completed handler, I tried:
foreach (var user in loadOp.Entities)
context.Users.Attach(user);
And I get an exception that says an entity with that name is already attached.
Yet, both context.Users and context.EntityContainer are empty.
Are you sure you are using the same instance of the context in all cases? What does context.EntityContainer.GetEntitySet<User>().Count say?
Does LoadOperation<User>.HasError return true? If so, what is the error?

Concurrency with a custom DomainService

I have created a class that inherits for DomainService and have a Silverlight app that uses System.ServiceModel.DomainServices.Client to get a DomainContext. I have also created POCO DataContracts that are used in the DomainServices's Query, Update, Insert, and Delete methods. I also have a ViewModel that executes all the LoadOperations. And now I'm at the part of my app where I want to add new Entities to the generated EntitySets but am unsure about what's going to happen when one user creates new and sets the Key value; all while another user creates a similar entity with the same Key value.
I have seen in the documentation that an ObjectContext is used, but in my situation I was not able to use the EntityFramework model generator. So I had to create my datacontracts by hand.
So I guess my question is, is there any way I can force other silverlight apps to update on database change?
When you make a Save operation to your DomainContext, depending on the load behavior, it will automatically refresh.
TicketContext.Load(TicketContext.GetTicketByIdQuery(ticketId),
LoadBehavior.RefreshCurrent,
x =>
{
Ticket = x.Entities.First();
Ticket.Load();
((IChangeTracking) TicketContext.EntityContainer).AcceptChanges();
}, null);
Here I've set the LoadBehavior to RefreshCurrent. When you make a save, RIA will send the entity back across the wire to the client and merge the changes with the entity already cached on your client side context. I don't know if that quite answers your question or not however.

Entity Framework Refresh Entity Does Not Refresh Its Foreign Keys

When marking an entity for deletion, and saving changes, sometimes the operation fails due to foreign key constraints for example. In this case, i notify the user and refresh the entity. The problem is that the entity does not get fully refreshed - its foreign keys stays empty.
For example :
//EditableEntity.SomeCollection --> Populated...
ContextManager.CurrentObjectContext.DeleteObject(EditableEntity);
try
{
ContextManager.CurrentObjectContext.SaveChanges();
}
catch (Exception err)
{
ContextManager.CurrentObjectContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, EditableEntity);
}
//EditableEntity.SomeCollection --> Empty!!
Please help,
Thanks,
Oran
Well, it seems that after recreating the EDM, and rebuilding the project, the above procedure works fine.
To conclude the solution : When marking an object for deletion, it behaves as it was 'detached' from the object context losing its related parents and collections but is actually still in the 'attached' state. If the delete opertaion is canceled of any reason, the usage of this object related objects and collections, will raise an exception. To solve this we need to 'attach' the object back to the object context. The problem is that the object is still marked as 'attached'.
To solve this simply refresh the object using :
ContextManager.CurrentObjectContext.Refresh(RefreshMode.StoreWins, item);
Hope it helps,
Oran

Resources