I am keeping local entities in breeze cache, how can i delete them from the cache with out going to the server?
in the documentation it states
Deleting an entity
You delete an entity by changing its EntityState to “Deleted” like this:
1
someEntity.entityAspect.setDeleted(); // mark for deletion
setDeleted does not destroy the object locally nor does it remove the entity from the database. The entity simply remains in cache in its new “Deleted” state … as changed and added entities do. A successful save does delete the entity from the database and remove it from cache.
You can do this by detaching the entity from entity manager by calling manager's detachEntity method :
manager.detachEntity(entity);
The detached entity will eventually be garbage collected.
Refer to Breeze-Inside Entity
You can do
entity.setDeleted();
and then call
saveChanges method.
if you want to save it in DB.
if not want to go to server
manager.detachEntity(nameofYourEntity);
Related
If you create entity client-side through EntityManager.createEntity(), when exporting, breeze generates tempKeys and assigns them to newly created entities.
But some entities are created server-side, but not saved(just created with new operator). Breezejs client is making a query. EntityState of fetched entities is Unchanged. PrimaryKey Id=0, it was just created, but not saved to db.
When you make exportEntities on manager it does not generate tempKeys, and entity Id remains zero. I tried to manually set EntityState to Added on that entities before exporting, but still tempKeys are not generated.
Any ideas how to properly export not-saved entity which came from server-side?
I think Breeze only creates a new temp key when the entity is attached to the EntityManager with state 'Added'. So try this:
entityManager.detachEntity(entity);
entityManager.attachEntity(entity, breeze.EntityState.Added);
That should cause a new temp key to be generated.
I am able to create an entity object, set some default values and add it to the manager. Now I want to track the changes to it using manager.hasChanges(). For some reason, this always returns true. Should I be checking something else too when tracking newly created entities that are not in the database?
EntityManager treats an entity with EntityState.Added as a 'changed' entity. This is why HasChanges will always return true.
In this case, you should listen to EntityManager.entityChanged event to track changes.
See http://www.breezejs.com/sites/all/apidocs/classes/EntityManager.html#event_entityChanged
As soon as you 'Add' it to the EntityManager, by definition, it has changes because it is in an 'Added' state. To breeze 'hasChanges' means that it needs to be saved because it is 'different' from what was provided by your persistence service ( in this case the entity hasn't yet been 'saved').
What you can do is 'Attach' your entity to the EntityManager in an 'Unchanged' state.
myEntityManager.attachEntity(newEntity, breeze.EntityState.Unchanged);
In this case, any changes you make will work the way you want, BUT ...
Breeze will now consider your newly attached entity to already have been persisted and if you try to save it, then the save will fail because breeze will try to 'modify' the persisted entity instead of creating a new one.
If you really want to accomplish your request you will need to use 'attach' but also keep track of these entities and mark them 'added' before you attempt to save them.
I am trying to add an entity to the DB. Once I have added it, I want to detach it, so I can manipulate the object safely without making any changes to the DB. After calling context.SaveChanges() I do the following to detach the entity:
// save
context.Stories.Add(story);
// attach tags. They already exists in the database
foreach(var tag in story.Tags)
context.Entry(tag).State = System.Data.EntityState.Unchanged;
context.SaveChanges();
context.Entry(story).State = System.Data.EntityState.Detached;
However, changing the entity state to DETACHED will remove all related entities associated with the my entity. Is there a way to stop this ?
If I don't detach the entity, all my changes are sent to the DB next time I call context.SaveChanges()
Thanks!!
There is no way. It is limitation of EF. Your options are:
Not using the same context for another save (single context instance = single save)
Retrieve the entity from database again using another context instance which will not be used for saving
Create deep clone of your entity and use the clonned one (deep clone is done by serialization and immediate deserialization = your entity graph must be serializable)
I think there are two ways to approach this problem:
Purist: retrieving entities from a DbContext and modifying them without saving is a misuse of the tools and the architecture. Use a DTO instead.
Pragmatic: you can use AsNoTracking() to retrieve an entity graph that will not be tracked by the context for changes.
How can I access deleted entity in self tracking entities graph?
I understand that in case I use MarkAsDeleted on the objects contained in a collection they are moved to ObjectsRemovedFromCollectionProperties of the parent entity, but how to access deleted objects that are not in a collection?
Thanks
Jakub
Deleted reference is not directly exposed but you can debug ApplyChanges to see where it comes from. If you want to have the reference to deleted object you can simpy use return value from MarkAsDeleted.
I have an oData generated DataServiceContext and I am successfully adding entities to it. I need to add a whole load of entities and then commit them in a single SaveChanges with the Batch option set at the end. This is all fine, until I come to query it before the save changes.
Outline is:
Create a new entity
Add it to the DataServiceContext
Run a query on the context looking for the item I have just added - IT IS NOT FOUND
My previous work with EF4 would suggest that if this was an Entity Context, all would be fine, but because this is a Service Context I cannot query for an entity that has been added but not saved to the service.
Is this the case?
DataServiceContext is basically just a small helper. Running any query against it will run the query on the server directly, the client will not try to fixup the data in any way. Since you're changes haven't made it to the server yet (SaveChanges was not called yet), the query will not return the newly added entities.
If you really need to list the entities you've added before SaveChanges, you could use the DataServiceContext.Entities collection which will return EntityDescriptor for all entities tracked by the context. You can list those added by looking for those with state Added.