28

I have a database and iam trying to use dapper with Core Identity to make queries to database. but i am stuck at this point. I am using a User from the interface of identityUser:

public class User : IdentityUser
{

}

The with a making a custom user store for CRUD with dapper.

 public class UserStore : IUserStore<User>
{
    private readonly string connectionString;

    public UserStore(IConfiguration configuration)
    {
        connectionString = configuration.GetValue<string>("DBInfo:ConnectionString");
    }

    internal IDbConnection Connection
    {
        get
        {
            return new SqlConnection(connectionString);
        }
    }
    public Task<IdentityResult> CreateAsync(User user, CancellationToken cancellationToken)
    {
**// HOW TO I RETURN A USER WITH DAPPER HERE?**
    }

    public Task<IdentityResult> DeleteAsync(User user, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public Task<User> FindByIdAsync(string userId, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task<User> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task<string> GetUserIdAsync(User user, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task<string> GetUserNameAsync(User user, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

    public Task<IdentityResult> UpdateAsync(User user, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

thanks!

5
  • I'd advice you to read the documentation: github.com/StackExchange/Dapper Its basically an extension Library on SqlConnection/IDbConnection Commented Mar 31, 2017 at 13:34
  • 2
    What are you actually asking? How to write creation logic against a database structure that we haven't seen? This question is far too unclear and broad to answer.
    – David L
    Commented Mar 31, 2017 at 19:36
  • 'public Task<IdentityResult> CreateAsync(User user, CancellationToken cancellationToken) { // HOW TO I RETURN A USER WITH DAPPER HERE? }'
    – Gonzalo
    Commented Apr 3, 2017 at 6:08
  • 1
    Did you have a look at Identity.Dapper Commented Apr 13, 2017 at 16:29
  • 2
    I'd like to just point out that, whatever solution you'll implement, accordingly to this issue, Asp.net Identity expects that the stores are following an unit of work pattern or you'll probably have subtle issues down the road
    – Ghidello
    Commented May 14, 2017 at 9:29

1 Answer 1

31

Please take a look at a project that i recently uploaded on GitHub - https://github.com/giorgos07/Daarto It's exactly what you need.

6
  • 1
    Do you happen to have a version with Core 2.0? I've been trying to find an example somewhere but no luck. Trying to create a basic project with Dapper+AspNet Core Identity 2.0
    – Bagzli
    Commented Aug 28, 2017 at 21:17
  • 1
    @Bojan i have updated the project to ASP.NET Core 2.1 Commented Sep 13, 2018 at 15:52
  • Great work! I think this should be a NuGet package.
    – Bedir
    Commented Feb 12, 2021 at 20:47
  • @GiorgosManoltzas How can I extend Identity User class? Can you give a example? Commented Jul 9, 2021 at 17:13
  • 1
    @vibs2006 thank you for your comment. Please take a look at the repo and NuGet. I added support for netstandard2.1 - net5.0 and net6.0 Commented Aug 20, 2022 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.