Base solution for your next web application
Open Closed

SaveChanges before changing to a host UnitOfWork scope #11534


User avatar
0
atideveloper created

Hi,

I'm working on adding notifications to our application. All in all things are going fine but I've noticed some strange behaviour appearing.

I noticed that unless I manually call SaveChanges before publishing notifications some fields are no longer automatically set by ABP. These include TenantId, LastModfierUserId and most likely also CreatorUserId. After investigating a bit I found that this happens because the publish notification logic switches to a Host unit of work context and performs a SaveChanges. This leads to tenantId not being available.

I then found documentation stating that tenantId should be manually set which resolves that part of the problem. However, nothing is said regarding LastModifierUserId nor CreatorUserId as far as I can tell.

My question, therefore, is: Is it recommended to call SaveChanges before switching to a host unit of work in order to prevent problems like these?

Thank you


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @atideveloper

    This mostly depends on your code. Could you share your code block ?

  • User Avatar
    0
    atideveloper created

    We are not really doing anything all that strange. Basically, we are doing something similar to:

    public async Task HttpPostService()
    {
        EntityImpl item = await repository.Get(id);
        item.Text = "New text";
        notificationPublisher.PublishAsync("EntityChanged",...);
     }
    

    After we started calling publish async, the LastModifierUserId is no longer set. LastModificationTime is set though. We found it strange that simply adding that one function call lead to this which is why we opened a question. If I add a call to SaveChanges before the publish all values are populated like before.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes, in this case, you need to call SaveChanges.