Note that there are some explanatory texts on larger screens.

plurals
  1. POBreezeJS: Server added object showing as added in client after save changes
    primarykey
    data
    text
    <p>I have a breeze controller that adds an entity during the save changes that the client did not submit. </p> <pre><code> protected override bool BeforeSaveEntity(EntityInfo entityInfo) { if (entityInfo.Entity.GetType() == typeof(User)) { if (entityInfo.EntityState == EntityState.Added) { User user = entityInfo.Entity as User; OrganizationUser orgUser = new OrganizationUser() { Enabled = true, OrganizationId = User.OrgId, User = user }; user.OrganizationUsers.Add(orgUser); } return true; } throw new InvalidOperationException("You can not use this service to modify an entity of type: " + entityInfo.Entity.GetType().Name); } </code></pre> <p>When the response is returned the client side breeze manager sets the state of the server side added object as 'Added'. The OrganizationUser object above ends up with an Added state on the client. It then gets submitted in the next SaveChanges. Is this a bug?</p> <p>Here is the response from the first save:</p> <pre><code>{ "$id": "1", "$type": "Breeze.WebApi.SaveResult, Breeze.WebApi", "Entities": [{ "$id": "2", "$type": "LeonardoMD.Server.Api.Security.Admin.User, LeonardoMD.Server", "Id": 9176, "Email": "SearchProviderA@leonardoMD.com", "FirstName": "SearchProviderA", "LastName": "SearchProviderA", "OrganizationUsers": [{ "$id": "3", "$type": "LeonardoMD.Server.Api.Security.Admin.OrganizationUser, LeonardoMD.Server", "UserId": 9176, "User": { "$ref": "2" }, "OrganizationId": 421, "Enabled": true }] }], "KeyMappings": [{ "$id": "4", "$type": "Breeze.WebApi.KeyMapping, Breeze.WebApi", "EntityTypeName": "LeonardoMD.Server.Api.Security.Admin.User", "TempValue": -1, "RealValue": 9176 }] </code></pre> <p>}</p> <p>Here is the submission of the second save:</p> <pre><code>{ "entities": [{ "Id": -2, "CreateDate": null, "CreateUserId": null, "ModifyDate": null, "ModifyUserId": null, "Email": "SearchProviderB@leonardoMD.com", "FirstName": "SearchProviderB", "LastName": "SearchProviderB", "BirthDate": null, "GenderId": null, "AddressLine1": null, "AddressLine2": null, "City": null, "State": null, "StateId": null, "PostalCode": null, "CountryId": null, "DayPhone": null, "DayPhoneExtension": null, "Password": null, "SecretQuestion": null, "SecretAnswer": null, "Enabled": null, "AcceptTermsDate": null, "entityAspect": { "entityTypeName": "User:#LeonardoMD.Server.Api.Security.Admin", "defaultResourceName": "Users", "entityState": "Added", "originalValuesMap": {}, "autoGeneratedKey": { "propertyName": "Id", "autoGeneratedKeyType": "Identity" } } }, { "UserId": 9176, "OrganizationId": 421, "Enabled": true, "entityAspect": { "entityTypeName": "OrganizationUser:#LeonardoMD.Server.Api.Security.Admin", "defaultResourceName": "OrganizationUsers", "entityState": "Added", "originalValuesMap": {}, "autoGeneratedKey": null } }], "saveOptions": {} </code></pre> <p>}</p> <p>Notice the second entity is the entity returned in the response of the previous save changes. It's entityState is set to Added.</p> <p>I have a work-around but its fragile and would need to be written special to every circumstance where the server returns a new entity after a save. Is there a way to set Breeze to acceptChanges on all new entities returned from the server as the the response to a saveChanges call?</p> <pre><code> manager.saveChanges() .then(function (saveResult) { $.each(saveResult.entities, function (i, entity) { if (entity.organizationUsers &amp;&amp; entity.organizationUsers().length &gt; 0) $.each(entity.organizationUsers(), function (index, orgUser) { orgUser.entityAspect.acceptChanges(); }); entity.entityAspect.acceptChanges(); }); dfd.resolve(); }) .fail(function (error) { _this.handleError(context, promise, error); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload