.NET技术-2.0. 操作数据库-EF
项目参见:
EF安装配置 参见:
https://www.cnblogs.com/1285026182YUAN/p/10772330.html
https://www.cnblogs.com/1285026182YUAN/p/10749872.html
1. 查询数据
var queryRes1 = DB.TblOrder.Where(t => true).ToList();
2. 新增数据
#region == 新增 数据 == //新增 单条 Models.TblOrder model = new Models.TblOrder() { Amount = 10, Uptime = DateTime.Now, OrderCode = "222222", UserId = 2, Text = "aaaa" }; DB.TblOrder.Add(model); DB.SaveChanges(); //新增 多条 List<Models.TblOrder> list = new List<TblOrder>() {new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "222222", UserId = 2 ,Text="aaaa"}, new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "333333", UserId = 2 ,Text="aaaa"} }; DB.TblOrder.AddRange(list); DB.SaveChanges(); #endregion
3. 修改数据
#region == 修改 数据 == //修改 单条 model = queryRes1.First(); model.Text = "update_" + DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToLongTimeString(); DB.Entry(model).State = Microsoft.EntityFrameworkCore.EntityState.Modified; //也可以 DB.SaveChanges(); //修改 多条 list = queryRes1.Skip(2).Take(3).ToList(); list.ForEach(t => { t.Text = "update_" + DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToLongTimeString(); //DB.Entry(t).State = Microsoft.EntityFrameworkCore.EntityState.Modified; }); DB.SaveChanges(); #endregion
或
#region == 修改2 数据 == //修改 单条 model = queryRes1.First(); model.Text = "update2_" + DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToLongTimeString(); DB.TblOrder.Update(model); DB.SaveChanges(); //修改 多条 list = queryRes1.Skip(2).Take(3).ToList(); list.ForEach(t => { t.Text = "update2_" + DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToLongTimeString(); }); DB.TblOrder.UpdateRange(list); DB.SaveChanges(); #endregion
4. 删除数据
#region == 删除 数据 == //删除 单条 model = queryRes1.First(); DB.Entry(model).State = Microsoft.EntityFrameworkCore.EntityState.Deleted; DB.SaveChanges(); //修改 多条 list = queryRes1.Skip(2).Take(3).ToList(); list.ForEach(t => { DB.Entry(t).State = Microsoft.EntityFrameworkCore.EntityState.Deleted; }); DB.SaveChanges(); #endregion
或
#region == 删除2 数据 == queryRes1 = DB.TblOrder.Where(t => true).ToList(); //删除 单条 model = queryRes1.First(); DB.TblOrder.Remove(model); DB.SaveChanges(); //修改 多条 list = queryRes1.Skip(2).Take(3).ToList(); DB.TblOrder.RemoveRange(list); DB.SaveChanges(); #endregion
5. 开启事务
// GET api/values [HttpGet] public ActionResult<IEnumerable<string>> Get() { using (IDbContextTransaction transaction = DB.Database.BeginTransaction()) { try { //新增 多条 List<Models.TblOrder> list = new List<TblOrder>() {new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "9", UserId = 2 ,Text="aaaa"}, new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "8", UserId = 2 ,Text="bbbbb"}, new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "7", UserId = 2 ,Text="ccccc"}}; DB.TblOrder.AddRange(list); DB.SaveChanges(); //throw new Exception(); List<Models.TblOrder> list2 = DB.TblOrder.Where(t => true).ToList(); list2.ForEach(t => { t.Text = "bkas"; }); DB.TblOrder.UpdateRange(list2); DB.SaveChanges(); Trans(transaction);//事务做为参数传递。 //提交事务 transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); Console.WriteLine("Error occurred."); } } return new string[] { "value1", "value2" }; }
//带事务参数的方法 private void Trans(IDbContextTransaction transaction) { //新增 多条 List<Models.TblOrder> list = new List<TblOrder>() {new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "213", UserId = 2 ,Text="aaaa"}, new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "214", UserId = 2 ,Text="bbbbb"}, new TblOrder() { Amount = 10, Uptime= DateTime.Now, OrderCode = "215", UserId = 2 ,Text="ccccc"}}; DB.TblOrder.AddRange(list); DB.SaveChanges(); }
OK