Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top