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

 

 

 

 

OK

 

转载于:https://www.cnblogs.com/1285026182YUAN/p/10774300.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值