Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug when breeze processing the saveResult from saveChanges()? #150

Open
rastographics opened this issue May 26, 2016 · 4 comments

Comments

@rastographics
Copy link

I am modifying the saveResult in AfterSaveEntities on server. (.Net, EF6). Everything is working good except a new problem coming up.

I am modifying an entity (client-side) to make the entity's foreign key null. The saveresult comes back from the server with a null foreign key on the entity as expected (checking the json response in Chrome Dev Tools).

When stepping through breeze.debug.js, the data returned from the ajax.post call that is made has the correct "null" value for the foreign key.

Somewhere though, and I cannot figure out where, breeze is "throwing away" the new "null" value and setting the foreign key back to the original value it had before I called saveChanges().

If I refresh my entire page, breeze grabs the correct data from server (the null foreign key is what is getting saved in the database).

But how come this foreign key field is getting reset after being returned into breeze correctly by the server.

Could this be a bug somewhere?

I can post some code but was wondering first if anyone recognized this behavior or if it is expected.

@steveschmitt
Copy link
Member

steveschmitt commented Jun 3, 2016

This doesn't seem like correct behavior. Perhaps breeze thinks the foreign key value is just missing from the results, so it doesn't overwrite the current client value? Please post some code if you can.

Thanks for pointing this out.

@rastographics
Copy link
Author

rastographics commented Jun 5, 2016

I refactored my code a bit to fit all in one place so hope I didn't introduce typos in doing that.

removeRegistreeFromReservation is the starting point. I show in the code comments where the entity's foreign key value gets set back to the original value instead of null.

Let me know if you need to see server side code too. But I am certain that the saveResult that is returned on the wire has the correct "null" value for the entity (registree.reservationId == null).


var masterEm = new breeze.EntityManager(ENV.apiEndpoint + "/breeze/data");

function removeRegistreeFromReservation(registree) {
    //registree is an entity from masterEm 

    var registreeKey = registree.entityAspect.getKey();
    var sandboxEm = masterEm.createEmptyCopy();

    //export registree from masterEm to sandboxEm
    sandboxEm.importEntities(masterEm.exportEntities(
        registree, {
            asString: false,
            includeMetadata: false
        }));

    //get the freshly-imported registree from the sandboxEm
    var sandboxedRegistree = sandboxEm.getEntityByKey(entityKey.entityType, entityKey.values);

    //set the foreign-key to null to remove it from the registration
    sandboxedRegistree.reservationId = null;


    return saveSandboxedEntity(sandboxedRegistree)
        .catch(errorOnSave);
}





function saveSandboxedEntity(sandboxedEntity) {

    //get the EM this entity belongs to
    var sandboxEm = sandboxedEntity.entityAspect.entityManager;

    //get the entity type name so we can get this entity from the masterEm after importing it back into masterEm 
    var entityType = sandboxedEntity.entityAspect.getKey().entityType.shortName;

    return sandboxEm.saveChanges()
        .then(function (saveResult) {
            //**** BY THIS POINT, BREEZE HAS RESET THE sandboxedRegistree.reservationId back to the original ID !!! ******************************


            //import changes into master (from http://breeze.github.io/doc-cool-breezes/import-save-results.html)
            updateMasterWithSaveResult(sandboxEm, saveResult);

            var masterEntity = getEntityById(entityType, sandboxedEntity.entityAspect.getKey().values);
            if (masterEntity) return masterEntity;

        })
        .catch(errorOnSave);

}


//function copied from http://breeze.github.io/doc-cool-breezes/import-save-results.html
function updateMasterWithSaveResult(sourceEm, saveResult) {
    var imports = [];
    var deletes = [];
    saveResult.entities.forEach(function (entity) {
        if (entity.entityAspect.entityState.isDetached()) {
            deletes.push(entity);
        } else {
            imports.push(entity);
        }
    });
    var exported = sourceEm.exportEntities(imports, {
        includeMetadata: false,
        asString: false // as JSON
    });
    masterEm.importEntities(exported);

    deletes.forEach(function (detached) {
        var entity = masterEm.getEntityByKey(detached.entityAspect.getKey());
        entity && entity.entityAspect.setDetached();
    });
}

@rastographics
Copy link
Author

@steveschmitt anything else I can post that would help find the issue?

@rastographics
Copy link
Author

@steveschmitt @wardbell I know this issue has been sitting for a long time, but I never could figure out an explanation besides it being some type of bug in breeze.

I recently have come back to this problem and really need to tackle it. Any ideas, or hints where I can find an answer to this behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants