question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Module Zero UserManager CreateAsync - Id of created entity not set on user object

See original GitHub issue

The following code is provided by Module Zero and is found in UserAppService.cs

public override async Task<UserDto> Create(CreateUserDto input)
{
	CheckCreatePermission();

	var user = ObjectMapper.Map<User>(input);
	
	user.TenantId = AbpSession.TenantId;
	user.Password = new PasswordHasher().HashPassword(input.Password);
	user.IsEmailConfirmed = true;

	//Assign roles
	user.Roles = new Collection<UserRole>();
	foreach (var roleName in input.RoleNames)
	{
		var role = await _roleManager.GetRoleByNameAsync(roleName);
		user.Roles.Add(new UserRole(AbpSession.TenantId, user.Id, role.Id));
	}
	
	CheckErrors(await _userManager.CreateAsync(user));
	
	return MapToEntityDto(user);
}

Should _userManager.CreateAsync(user) populate the user object Id after creation? If I check the value of user.Id after CreateAsync, the value is still zero 0.

CheckErrors(await _userManager.CreateAsync(user));
var x = user.Id; /// This is equal to zero 0

My understanding was that CreateAsync would update the user object to include the Id value of the inserted entity. Is this a bug? Does this have something to do with Async?

Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
acjhcommented, Nov 30, 2017

My bad. I see that MapToEntityDto(user) is called before ABP has a chance to save changes.

1reaction
systemtrendscommented, Nov 30, 2017

Thanks for the help.

Yes, I did check on the client side. Using the attribute, [UnitOfWork], I still get an id value of zero 0 on the client side.

If I add the line UnitOfWorkManager.Current.SaveChanges(); to the original Create method as detailed above, then an Id value is returned on the client side.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UserManager.CreateAsync does not generate Id
I just upgraded my MVC 5 application (was previously MVC 3 with SimpleMembership ) to ASP.NET Identity 2.0 and I can work with...
Read more >
Module Zero UserManager CreateAsync - Id of created ...
The following code is provided by Module Zero and is found in UserAppService.cs public override async Task<UserDto> Create(CreateUserDto ...
Read more >
Custom User Management in ASP.NET Core MVC ...
We will start off by creating a new ASP.NET Core 3.1 MVC Project with Authentication Mode selected as Individual User Accounts.
Read more >
Articles Tutorials | AspNet Boilerplate
User Login​​ Module Zero defines LoginManager which has a LoginAsync method used for logging into the application. It checks all logic for the...
Read more >
Issue when check identity FindByNameAsync on debug it ...
Try changing this line to: var Identityuser = userManager.FindByNameAsync(request.UserName).Result;. Please sign in to rate this answer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found