一、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 }); }
效果: