0

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());
    }
Robert Achmann
  • 1,986
  • 3
  • 40
  • 66
  • can you post the code in the `FindByNameAsync` method? – SOfanatic Jun 11 '15 at 00:50
  • I will update my question above to include that code - just a sec - done – Robert Achmann Jun 11 '15 at 00:56
  • I don't see that code being the problem either, the issue has to be somewhere else, maybe the sql query that you are using? if all you need to get is the Id of the newly created entity then I would suggest following this approach: http://stackoverflow.com/questions/8270205/how-do-i-perform-an-insert-and-return-inserted-identity-with-dapper – SOfanatic Jun 11 '15 at 11:43
  • I'm thinking something's wonky with the DapperExtension - I'll get the latest version of dapper and dapperextensions and see if that's it. I'll also try the basic Dapper Query call. If that's still wrong, then either my class is suspect or my version of SQLMapper is busted... – Robert Achmann Jun 11 '15 at 11:55
  • Ok, it looks like the issue is with my use of DapperExtensions - perhaps my class? I'll see – Robert Achmann Jun 11 '15 at 12:50
  • Looks like, even with [Key] attribute assigned to the class' Id field, DapperExtensions still sets the field to be NonKey and Ignored...grrr – Robert Achmann Jun 11 '15 at 13:13
  • Never mind...I had a mapper class for my user class that specifically told DapperExtensions to ignore my Id field... It's been a while since I used this library... – Robert Achmann Jun 11 '15 at 13:28

1 Answers1

0

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.

Robert Achmann
  • 1,986
  • 3
  • 40
  • 66