/NETCore.DapperKit

Dapper extensions

Primary LanguageC#MIT LicenseMIT

NETCore.DapperKit

Easy use Dapper like ef with DapperKit.


Install with nuget

  • To install NETCore.DapperKit, run the following command in the Package Manager Console
Install-Package NETCore.DapperKit -Version 2.0.0

Add Dapperkit service in starup.cs

  • Using DapperKit extensions
using NETCore.DapperKit.Extensions;
  • Add Dapperkit service
 public void ConfigureServices(IServiceCollection services)
      {
          // Add framework services.
          services.AddMvc();

          // Add DapperKit
          services.AddDapperKit(optionsBuilder =>
          {
              optionsBuilder.UseDapper(new DapperKitOptions()
              {
                  ConnectionString = "Data Source=.;User ID=sa;Password='123456';Initial Catalog=DapperDb;MultipleActiveResultSets=True;"
              });
          });
      }

User DapperKit in asp.net core mvc controller

  • Init DapperContext
public class AccountController : Controller
{

    private readonly IDapperContext _DapperContext;

    public AccountController(IDapperContext context)
    {
        _DapperContext = context;
    }
    ...
}
  • Use Dapper Like EF
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
    ...
    var users = await _DapperContext.DbSet<SysUser>()
                                    .Select<SysUserRole>((u, r) => new SysUser() { Id = u.Id, IsAdmin = u.IsAdmin, LoginName = u.LoginName, LoginPwd = u.LoginPwd, CreateTime = u.CreateTime, UserName = u.UserName, UserRoleNo = u.UserRoleNo, RoleName = r.Name })
                                    .Join<SysUserRole>((u, r) => u.UserRoleNo == r.No)
                                    .Where(m => m.LoginName == model.Account && m.LoginPwd == model.Password)
                                    .ToListAsync<SysUser>();
    ...                                 
}

LICENSE

MIT LICESE