27

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!

Gonzalo
  • 322
  • 1
  • 3
  • 13
  • I'd advice you to read the documentation: https://github.com/StackExchange/Dapper Its basically an extension Library on `SqlConnection`/`IDbConnection` – Joel Harkes Mar 31 '17 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 Mar 31 '17 at 19:36
  • 'public Task CreateAsync(User user, CancellationToken cancellationToken) { **// HOW TO I RETURN A USER WITH DAPPER HERE?** }' – Gonzalo Apr 03 '17 at 06:08
  • 2
    I'd like to just point out that, whatever solution you'll implement, accordingly to this [issue](https://github.com/aspnet/Identity/issues/1113), 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 May 14 '17 at 09:29
  • 1
    Did you have a look at [Identity.Dapper](https://www.nuget.org/packages/Identity.Dapper/ "Identity.Dapper") – AdroitOldMan Apr 13 '17 at 16:29

1 Answers1

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.

Giorgos Manoltzas
  • 1,710
  • 5
  • 24
  • 34
  • 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 Aug 28 '17 at 21:17
  • 1
    @Bojan i have updated the project to ASP.NET Core 2.1 – Giorgos Manoltzas Sep 13 '18 at 15:52
  • Great work! I think this should be a NuGet package. – Bedir Feb 12 '21 at 20:47
  • @GiorgosManoltzas How can I extend Identity User class? Can you give a example? – GinCanhViet Jul 09 '21 at 17:13
  • @GiorgosManoltzas can you pl update your project to ASP.NET 6.0 ? Thanks again for such a helper to Dapper Community which microsoft ignores intentionally. Microsoft is doing its best to avoid Dapper – vibs2006 Aug 15 '22 at 08:10
  • 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 – Giorgos Manoltzas Aug 20 '22 at 14:51