Base solution for your next web application
Open Closed

Unit Of Works, transactions and Id #1643


User avatar
0
gpcaretti created

Hi, I am trying to implement a method into a service that create a user and then assign a role to it.

The problem is that to assign the role, I need the User.Id of the created user and I have not it but any FindBy into the same transaction return null due to the fact (I think) I am into the unit of work:

public class UserAppService : MyNewHouseAppServiceBase, IUserAppService {
  // ....
 public async Task CreateAdmin(CreateUserInput input) {
        var user = input.MapTo<User>();
       (await UserManager.CreateAsync(user)).CheckErrors(LocalizationManager);

       var u = await UserManager.FindByNameAsync(input.UserName); // <-- this returns null!!!
       (await UserManager.AddToRoleAsync(u.Id, StaticRoleNames.Tenants.Admin)).CheckErrors(LocalizationManager);
   }
}

I can I get the user.Id by staying into the unit of work? I wish to have the operation all transaction.

Tnx gp


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

    Hi,

    You can save changes after creating the user like this

    CurrentUnitOfWork.SaveChanges();
    

    It does not break UnitOfWork.