Ejemplo n.º 1
0
        public override void Test()
        {
            using (DDDTestContext context = new DDDTestContext())
            {
                //var PoDetail = context.PoDetail.FirstOrDefault(p => p.Ponum == "TestPonum" && p.ItemNum == "TestItemnum" && p.Lot == "TestLot");
                //if (PoDetail != null)
                //{
                //    context.PoDetail.Remove(PoDetail);
                //    context.SaveChanges();
                //}
            }

            using (DDDTestContext context = new DDDTestContext())
            {
                context.PoDetail.Add(
                    new PoDetail()
                {
                    Ponum   = "TestPonum",
                    ItemNum = "TestItemnum",
                    Lot     = "TestLot",
                    Qty     = 1000,
                    Uprice  = (decimal)0.5,
                    Amount  = (decimal)(1000 * 0.5),
                    PoDetail_TotalGrnQty = new PoDetail_TotalGrnQty()
                    {
                        Ponum       = "TestPonum",
                        ItemNum     = "TestItemnum",
                        Lot         = "TestLot",
                        TotalGrnQty = 0
                    }
                });
                context.SaveChanges();
            }
            using (DDDTestContext context = new DDDTestContext())
            {
                var PoDetail = context.PoDetail.FirstOrDefault(p => p.Ponum == "TestPonum" && p.ItemNum == "TestItemnum" && p.Lot == "TestLot");
                if (PoDetail != null)
                {
                    PoDetail.PoDetail_TotalGrnQty.TotalGrnQty = 123;
                    context.SaveChanges();
                }
            }

            using (DDDTestContext context = new DDDTestContext())
            {
                var PoDetail = context.PoDetail.FirstOrDefault(p => p.Ponum == "TestPonum" && p.ItemNum == "TestItemnum" && p.Lot == "TestLot");
                if (PoDetail != null)
                {
                    WriteLine("采购单号【{0}】 物料编码【{1}】 批次【{2}】 采购数量【{3}】 单价【{4}】 金额【{5}】 收货数量【{6}】", PoDetail.Ponum, PoDetail.ItemNum, PoDetail.Lot, PoDetail.Qty, PoDetail.Uprice, PoDetail.Amount, PoDetail.PoDetail_TotalGrnQty.TotalGrnQty);
                }
            }
        }
Ejemplo n.º 2
0
        private void test()
        {
            List <Task> tasks = new List <Task>();

            using (DDDTestContext _dbContext = new DDDTestContext())
            {
                var po = _dbContext.Set <Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
                if (po != null)
                {
                    _dbContext.Set <Po>().Remove(po);
                    _dbContext.SaveChanges();
                }

                po = new Po()
                {
                    PoNum       = "PONUM1",
                    VenId       = "VENID1",
                    TotalAmount = 100
                };
                _dbContext.Set <Po>().Add(po);
                _dbContext.SaveChanges();
                WriteLine("新增采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
            }
            tasks.Add(Task.Factory.StartNew(() =>
            {
                using (DDDTestContext _dbContext = new DDDTestContext())
                {
                    using (DbContextTransaction transaction = _dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
                    {
                        try
                        {
                            var po = _dbContext.Set <Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
                            WriteLine("第一次修改 获取到数据采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
                            po.VenId += "第一次修改";
                            WriteLine("第一次修改 采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
                            _dbContext.SaveChanges();
                            transaction.Commit();
                        }
                        catch (DbUpdateException ex)
                        {
                            WriteLine("事务死锁:{0}", ex.Message);
                            WriteLine("事务死锁:{0}", ex.InnerException.InnerException.Message);
                        }
                        catch (Exception ex)
                        {
                            WriteLine("第一次更新出错:错误信息【{0}】", ex.Message);;
                            WriteLine(ex);
                            //transaction.Rollback();
                        }
                    }
                }
            }));
            tasks.Add(Task.Factory.StartNew(() =>
            {
                using (DDDTestContext _dbContext = new DDDTestContext())
                {
                    using (DbContextTransaction transaction = _dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
                    {
                        try
                        {
                            var po = _dbContext.Set <Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
                            WriteLine("第二次修改 获取到数据采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
                            po.VenId += "第二次修改";
                            WriteLine("第二次修改 采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
                            _dbContext.SaveChanges();
                            transaction.Commit();
                        }
                        catch (DbUpdateException ex)
                        {
                            WriteLine("事务死锁:{0}", ex.Message);
                            WriteLine("事务死锁:{0}", ex.InnerException.InnerException.Message);
                        }
                        catch (Exception ex)
                        {
                            WriteLine("第二次更新出错:错误信息【{0}】", ex.Message);;
                            WriteLine(ex);
                            //transaction.Rollback();
                        }
                    }
                }
            }));

            Task.WaitAll(tasks.ToArray());
            using (DDDTestContext _dbContext = new DDDTestContext())
            {
                var po = _dbContext.Set <Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
                WriteLine("修改后采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
                //_dbContext.Set<Po>().Remove(po);
                //_dbContext.SaveChanges();
            }


            /**
             * {
             *  WriteLine("======================================================================");
             *
             *  Thread.Sleep(500);
             *  using (DDDTestContext _dbContext = new DDDTestContext())
             *  {
             *      var po = _dbContext.Set<Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
             *      if (po != null)
             *      {
             *          _dbContext.Set<Po>().Remove(po);
             *          _dbContext.SaveChanges();
             *      }
             *
             *      po = new Po()
             *      {
             *          PoNum = "PONUM1",
             *          VenId = "VENID1",
             *          TotalAmount = 100
             *      };
             *      _dbContext.Set<Po>().Add(po);
             *      _dbContext.SaveChanges();
             *      WriteLine("新增采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
             *  }
             *
             *  using (DDDTestContext _dbContext = new DDDTestContext())
             *  {
             *      using (DbContextTransaction transaction = _dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
             *      {
             *          try
             *          {
             *              var po = _dbContext.Set<Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
             *              WriteLine("第一次修改 获取到数据采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
             *              po.VenId += "第一次修改";
             *              WriteLine("第一次修改 采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
             *              _dbContext.SaveChanges();
             *              transaction.Commit();
             *          }
             *          catch (DbUpdateException ex)
             *          {
             *              WriteLine("事务死锁:{0}", ex.Message);
             *          }
             *          catch (Exception ex)
             *          {
             *              WriteLine("第一次更新出错:错误信息【{0}】", ex.Message); ;
             *              WriteLine(ex);
             *              //transaction.Rollback();
             *          }
             *      }
             *  }
             *  using (DDDTestContext _dbContext = new DDDTestContext())
             *  {
             *      using (DbContextTransaction transaction = _dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
             *      {
             *          try
             *          {
             *              var po = _dbContext.Set<Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
             *              WriteLine("第二次修改 获取到数据采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
             *              po.VenId += "第二次修改";
             *              WriteLine("第二次修改 采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
             *              _dbContext.SaveChanges();
             *              transaction.Commit();
             *          }
             *          catch (DbUpdateException ex)
             *          {
             *              WriteLine("事务死锁:{0}", ex.Message);
             *          }
             *          catch (Exception ex)
             *          {
             *              WriteLine("第二次更新出错:错误信息【{0}】", ex.Message);
             *              WriteLine(ex);
             *              //transaction.Rollback();
             *          }
             *      }
             *  }
             *
             *  using (DDDTestContext _dbContext = new DDDTestContext())
             *  {
             *      var po = _dbContext.Set<Po>().FirstOrDefault(p => p.PoNum.Equals("PONUM1"));
             *      WriteLine("修改后采购订单,编号【{0}】,供应商【{1}】,合计金额【{2}】", po.PoNum, po.VenId, po.TotalAmount);
             *      //_dbContext.Set<Po>().Remove(po);
             *      //_dbContext.SaveChanges();
             *  }
             * }
             *
             **/
        }