Base solution for your next web application
Open Closed

CreatorUserId Set to Null #352


User avatar
0
ninomartini created

When a record is created, the framework populates the CreationTime correctly and sets the CreatorUserId to the logged in user correctly. When the record is modified, the framework populates the LastModificationTime and LastModifierUserId correctly. However, it also makes the CreaterUserId null after the record is modified. Is this by design? If yes, is there a way to keep the CreatorUserId from getting cleared?


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

    No, it should not be like that. I just tried to create a role in the role management page. Then I edited it, changing the name. When I check the database, there is no problem, CreatorUserId is there! I suspect that your DTO overrides it or somehow you're setting it to null. Because ABP does not set creation properties on update. Can you double check it. Also, try for the roles as I try. Thanks.

  • User Avatar
    0
    ninomartini created

    Thank you for the quick response. I discovered my issue and wanted to share it here to make sure I am performing the update correctly by your standards.

    I believe my issue was in my service method.

    Orginal Update Method which was updated the CreatorUserId to NULL:

    var group = input.MapTo<Group>();
    await _groupRepository.UpdateAsync(group);
    

    Corrected Update Method which now saves correctly:

    var group = await _groupRepository.FirstOrDefaultAsync(g => g.Id == input.Id);
     input.MapTo(group);
     await _groupRepository.UpdateAsync(group);
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    This is the most true way of it:

    var group = await _groupRepository.GetAsync(input.Id);
    input.MapTo(group);
    
    1. No need to call Update method since UOW automatically updates changed entities.
    2. We should always get the Entity from repository and overwrite with the new values. Why? Think that we added a new property to Group entity but not added to the input DTO. This way, new property is not effected, so our method will be forward compatible. If we don't get it from repository (as your first code), we always make this new property NULL! (Same is true for the CreatorUserId).

    Have a nice day.