如何在ASP.Net Core Identity中使用Dapper?

27

我有一个数据库,正在尝试使用Dapper和Core Identity向数据库进行查询。但我卡在这个点上了。我正在使用IdentityUser接口中的User:

public class User : IdentityUser
{

}

使用Dapper创建自定义用户存储进行CRUD操作。

 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();
    }
谢谢!

我建议你阅读文档:https://github.com/StackExchange/Dapper 它基本上是对SqlConnection/IDbConnection的扩展库。 - Joel Harkes
2
你实际上在问什么?如何编写针对我们尚未看到的数据库结构的创建逻辑?这个问题太不清楚和广泛,无法回答。 - David L
public Task<IdentityResult> CreateAsync(User user, CancellationToken cancellationToken) { // 我该如何在这里使用Dapper返回一个用户呢? } - Gonzalo
1
你看过Identity.Dapper了吗? - AdroitOldMan
2
我想指出的是,无论你实现什么解决方案,根据这个问题,Asp.net Identity都期望存储遵循工作单元模式,否则你可能会在未来遇到一些微妙的问题。 - Ghidello
1个回答

31

1
你是否有支持Core 2.0的版本?我一直在尝试寻找一个例子,但没有成功。我想用Dapper+AspNet Core Identity 2.0创建一个基本项目。 - Bagzli
1
@Bojan 我已将项目更新为 ASP.NET Core 2.1。 - Giorgos Manoltzas
干得好!我认为这应该是一个NuGet包。 - Bedir
@GiorgosManoltzas,我该如何扩展Identity User类?你能给个例子吗? - GinCanhViet
@GiorgosManoltzas,您能否将您的项目更新到ASP.NET 6.0?再次感谢您对Dapper社区的帮助,而微软则故意忽略它。微软正在尽其所能避免Dapper。 - vibs2006
1
@vibs2006,感谢您的评论。请查看代码库和 NuGet。我已经添加了对 netstandard2.1 - net5.0 和 net6.0 的支持。 - Giorgos Manoltzas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接