Base solution for your next web application
Open Closed

Set UserID within UnitOfWork #4957


User avatar
0
bertusvanzyl created

I am doing some housekeeping inside a background worker. Part of this is writing entries into certain tables etc. Some of these tables implement ICreationAudited, so I would like to set the UserID for the current UOW so that the CreatorUserId columns is automatically updated to an BackgroundWorker user on the tenant.

I can already set the TenantID using UnitOfWorkManager.Current.SetTenantId() when I start the using block, but I am not sure how to set the user ID as well.

Any advice?


1 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    SetTenantId is for Data Filters. To act as a user, see Overriding Current Session Values:

    using (_session.Use(1, 2))
    {
        var tenantId = _session.TenantId; // 1
        var userId = _session.UserId;     // 2
    
        // write entries into certain tables
        // the CreatorUserId column is automatically updated to a user in the tenant
    }