探索Dapper与EF Core等ORM框架的神器

admin2024-05-15  4

一、EF Core 

我使用的是MySQL需要使用到 MySql.Data.EntityFrameworkCorePomelo.EntityFrameworkCore.MySql包

二、Dapper

需要引入Dapper包

③使用DapperHelper类

[HttpGet]
[Route("/api/Dapper")]
public IActionResult Dapper()
{
    //返回数据列表
    IEnumerable<user> a = DapperHelper.Query<user>("select * from user");
    //异步返回数据列表
    Task<IEnumerable<user>> asynca = DapperHelper.QueryAsync<user>("select * from user");
    //返回单个数据
    user b = DapperHelper.QuerySingle<user>("select * from user where Id=@Id", new { Id = 1 });
    //异步返回单个数据
    Task<user> asyncb = DapperHelper.QuerySingleAsync<user>("select * from where Id=@Id", new { Id = 2 });
    //添加信息
    DapperHelper.Execute($"insert into user(Name,Email,Password,Phone,Address,AddTime,Age)" +
        $"values(@Name,@Email,@Password,@Phone,@Address,@AddTime,@Age)", new
        {
            Name = "夏婉莹",
            Email = "352565@qq.com",
            Password = "1456",
            Phone = "15659259642",
            AddTime = DateTime.Now.ToString(),
            Address = "山东省",
            Age = 18
        });
    // ASP.NET Core 将自动将其序列化为 JSON
    return Ok(new { Data = a });
}

 效果:

探索Dapper与EF Core等ORM框架的神器,第1张

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!