Skip to content

Commit with specific entities & BeforeSaveEntity issue #13

Description

@fayeeg

I am getting the following error when I call 'commitEntities' ( extended function to pass specific entities in the save )

Unable to locate a 'Type' by the name: 'function'. Be sure to execute a query or call fetchMetadata first.

If I call 'commit' then savechanges works fine.

Could you please help how I can save specific entities? Below are the code snippets.

I have extended unit-of-work.ts with the below function. Same as 'commit' but added entities parameter.

commitEntities(entities: Entity[]): Promise<Entity[]> {

    let saveOptions = new SaveOptions({ resourceName: 'savechangesentities' });

    return this.manager.saveChanges(entities, saveOptions)
        .then((saveResult) => {
            UnitOfWork.committedSubject.next(saveResult.entities);

            return saveResult.entities;
        });
}

At server-side, in the DefaultController.cs, I have the below

[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
_unitOfWork._contextProvider.BeforeSaveEntityDelegate = null;
return _unitOfWork.Commit(saveBundle, User.Identity.Name);
}

    **[HttpPost]
    public SaveResult SaveChangesEntities(JObject saveBundle)
    {
        _unitOfWork._contextProvider.BeforeSaveEntityDelegate = BeforeSaveEntity;
        return _unitOfWork.CommitEntities(saveBundle);
    }**

    private bool BeforeSaveEntity(EntityInfo info)
    {
        CurrentUser = Utils.GetCurrentUser(User.Identity.Name);
        if (info.EntityState == EntityState.Modified &&
            info.Entity is IAuditable)
        {
            var auditable = (IAuditable)info.Entity;
            auditable.ModifiedDateTime = DateTime.Now;
            auditable.ModifiedBy = CurrentUser.UserId;
            
            // Add property to map so that ContextProvider updates db
            // original values don't matter

            info.OriginalValuesMap["ModifiedDateTime"] = null;
            info.OriginalValuesMap["ModifiedBy"] = null;
            
        }

        if (info.EntityState == EntityState.Added && info.Entity is IOwner)
        {

            var entity = (IOwner)info.Entity;
            entity.CreatedDateTime = DateTime.Now;
            entity.CreatedBy = CurrentUser.UserId;
            
            info.OriginalValuesMap["CreatedDateTime"] = null;
            info.OriginalValuesMap["CreatedBy"] = null;
           
        }

        

        return true;

    }

this is how my save function is written

var changedEntities: Entity[] = this.em.getEntities(typeof BLAgencyLocalCharges, EntityState.Added || EntityState.Modified || EntityState.Deleted)
//.commitEntities(changedEntities)
return this.busyService.busy(this.unitOfWork.commit()).then(() => {
if (suppressConfirmation) return;
this.dialogService.messageBox('Success', 'Successfully saved data!', ['Ok']);

		}).then(() => {
			if (!deactivating) {
				// Navigate to saved model

			}

			return true;
		});

Note:
below is given in the constructor
this.em = unitOfWork.em;

Edited:

BeforeSaveEntity when used with default 'SaveChanges' it did not update the database though I can see the changes the entity during the debug.

Appreciate if you could advise how to achieve Audit trail in Temphire ref app.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions