.NET技术-2.0. 操作数据库-EF

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

 

 

5. 在接口中传参 修改

        public async Task<dynamic> PatchDataAsync(ANoticeRenewPatch data)
        {
            var model = DBAssist.a_notice_renew.SingleOrDefault(t => t.isDeleted == false && t.id == data.id);
            if (model == null) throw new Exception("未找到数据。");
            var oldEntry = DBAssist.Entry(model);
            oldEntry.CurrentValues.SetValues(data);
            await DBAssist.SaveChangesAsync();
            return oldEntry.Entity;
        }
 public class ANoticeRenewPatch
    {
        public int id { get; set; }
        public string title { get; set; }
        public string? content { get; set; }
        public string? cognition { get; set; }
        public string? tag { get; set; }
        public string? remark { get; set; }
        public string? link { get; set; }
        public DateTime? sendTime { get; set; }
        public string? state { get; set; }
    }

 

 

 

 

OK

 

posted @ 2019-04-26 14:57  无心々菜  阅读(234)  评论(0编辑  收藏  举报