C# Identity UserManager CreateAsync then FindByEmailAsync returns User with Id=0

1.9k views Asked by At

Identity with Dapper.

My user in my Identity is of type IUser< int >

When a user Registers, I create a user in my AccountController:

Register POST:

if (ModelState.IsValid)
{
    user = new MySiteUserRecord
    {
        UserName = model.UserName,
        AccessFailedCount = 0,
        ApplicationName = "Portal",
        Description = string.Empty,
        PhoneNumber = model.PhoneNumber ?? string.Empty,
        Email = model.Email
    };
    var result = await UserManager.CreateAsync(user, model.Password);
    if (result.Succeeded)
    {
       ...

If successful, I need to get the newly created user record, because the 'new' user class, present above, didn't know what it's Id is yet...

       user = await UserManager.FindByNameAsync(model.UserName);

When I look at this found user record, the user.Id = 0. But if I look at the DB, the row for this newly created user is the next Id, for example, 9 Even if I do a direct Db query, rather than use the UserManager's FindByNameAsync function, the Id returned is always 0.

I'm really stumped - I've probably missed something...

What do I need to do to get the user record with the assigned DB Id?

EDIT: My Find User function is:

    Task<MySiteUserRecord> IUserStore<MySiteUserRecord, int>.FindByNameAsync(string userName)
    {
        var predicate = Predicates.Field<MySiteUserRecord>(f => f.UserName, Operator.Eq, userName);
        return Task.Factory.StartNew(() => _sqlConn.GetList<MySiteUserRecord>(predicate).FirstOrDefault());
    }
1

There are 1 answers

0
Robert Achmann On BEST ANSWER

A mapper class for my user class specifically told DapperExtensions to ignore my Id field. Have to be careful when using other Dapper extensions and need to investigate how they interact with your system.