Mike
Mike

Reputation: 95

BreezeJS EntityManager.saveChanges() not calling acceptChanges()

Hello and thank you for your help.

I'm using

"breeze-bridge-angular": "^4.0.1",
"breeze-client": "^1.6.3",

The documentation for acceptChanges() says "Breeze calls this method after a successful save." and discourages calling it directly.

http://breeze.github.io/doc-js/entitymanager-and-caching.html

However, in my experience, as this simple code shows, it does not:

  let day = this.em.createEntity("Day", { 'note': '', 'owner_id': 1});
  await this.em.saveChanges();
  let changes = this.em.hasChanges(); // true
  this.em.acceptChanges();
  changes = this.em.hasChanges();  // false

The Entity Day is created just fine in the DB, but if acceptChanges() is not called manually, a subsequent saveChanges() will duplicate the previous creation of the Day Entity as its state is not reset to UNCHANGED.

Please help me understand what I am doing wrong.

Thanks so much! Mike

Upvotes: 0

Views: 108

Answers (1)

Mike
Mike

Reputation: 95

I went back to basics on the server side and discovered I was missing an attribute from my controller:

[BreezeController]

Without this attribute everything else continued to work (fetching metadata, queries, local create, saves were persisted to the DB) - however lacking this attribute the client side entity manager refused to mark a persisted entity as being properly saved because the response to the client is very different:

Adding this attribute, the server response now looks like this:

{
  "$id": "1",
  "$type": "Breeze.ContextProvider.SaveResult, Breeze.ContextProvider",
  "Entities": [
    {
      "$id": "2",
      "$type": "reflectionship_model.blah, blah-model",
      "Id": 8,
      "X": 999,
      "Y": "blah"
    }
  ],
  "KeyMappings": [
    {
      "$id": "3",
      "$type": "Breeze.ContextProvider.KeyMapping, Breeze.ContextProvider",
      "EntityTypeName": "blah_model.Blah",
      "TempValue": -1,
      "RealValue": 8
    }
  ],
  "DeletedKeys": [ ],
  "Errors": null
}

json result w/o [BreezeController]:

{
  "Entities": [
    {
      "id": 62,
      "datetimme": null,
      "blah": "999",
    }
  ],
  "KeyMappin‌​gs": [
    {
      "EntityTypeNam‌​e": "test_model.Day",
      ‌​"TempValue": -2,
      "Real‌​Value": 62
    }
  ],
  "Deleted‌​Keys": [ ],
  "Errors": nu‌​ll
} 

Steve Schmitt - thank you very much for your help as you were correct in your assessment which led me to the solution!

Upvotes: 1

Related Questions