Base solution for your next web application
Open Closed

UserId cannot be set to null through _session.Use #10724


User avatar
0
daws created
  • What is your product version? 10.3
  • What is your product type (Angular or MVC)? Angular
  • What is product framework type (.net framework or .net core)? ?Net Core
  • What is ABP Framework version? 6.3.1

Dear support, I am injecting objects through an IRepository, and the CreatorUserId is filled with my current UserId = 2 (auto detected by ABPSession) when inserted into DB.

I want to modify the UserId of the object, to set it to null. Following your documentation, I use the session.Use (allowing a "long?" parameter) method but when I set the 2nd parameter as null, the UserId is still equals to "2".

If I use a non null value (e.g. "10"), this new value will correctly inserted into db.

Is there any info related to the null, that I am not aware of ?

public void InsertOrUpdateAreas(IList areas)
{
using (_session.Use(_session.TenantId, null))

        {
            var tenantId = _session.TenantId; //2
            var userId = _session.UserId; //2 ==> abp gives "2" instead of null

            foreach (var area in areas)
                    _areaRepository.Insert(area);

            CurrentUnitOfWork.SaveChanges();
        }
    }

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

    Hi @daws

    UserId for audit fields is retrieved using this method https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.EntityFrameworkCore/EntityFrameworkCore/AbpDbContext.cs#L492. You can override it in your DbContext and allow null values. By default, it doesn't allow null values.

  • User Avatar
    0
    daws created

    Thanks for the hint but I am still stuck.

    If I override GetAuditUserId, of course I can force it to return null but I want to get a null value only in a specific call, not for all methods. In all other method (except the one listed in my other post), it is important to keep the UserId filled in.

    thus, I wanted to use this function https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Runtime/Session/AbpSessionBase.cs#L40 which is supposed to take nullable userId but which does not work.

    I halso have another possibility which is to give a boolean parameter to the dbcontext TestDbContext : AbpZeroDbContext once the program is started and, depending of this boolean parameter, returning null in GetAuditUserId(). But I can not find a way to give a parameter to the dbcontext when it is created.

    do you have any more tips ?

    thanks

            protected override long? GetAuditUserId()
            {
                if (AbpSession.UserId.HasValue &&
                    CurrentUnitOfWorkProvider != null &&
                    CurrentUnitOfWorkProvider.Current != null &&
                    CurrentUnitOfWorkProvider.Current.GetTenantId() == AbpSession.TenantId)
                {
                    return AbpSession.UserId;
                }
    
                return null;
            }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @daws

    Maybe you can set a parameter in HttpContext and return null only when that parameter is set. In that way, you can return null only for specific calls.

  • User Avatar
    0
    daws created

    modifing the httpcontext or overriding the abpsession was too much complex for my use case.

    I have found the solution, simply use

    using (_unitOfWorkManager.Current.DisableAuditing(new string[]{ AbpAuditFields.CreationUserId}))