예제 #1
0
 public static void insert(P_HoaDon objHD,List<P_DonDatHang> lstDDH)
 {
     using (var model=ModelConnector.Connector())
     {
         using(var _trans=model.Database.BeginTransaction())
         {
             try
             {
                 string _GuidID= Guid.NewGuid().ToString();
                 objHD.ID_HoaDon_CT = _GuidID;
                 model.P_HoaDon.Add(objHD);
                 foreach (var item in lstDDH)
                 {
                     P_HoaDonCT _ct = new P_HoaDonCT();
                     _ct.ID_HoaDon = _GuidID.ToString();
                     _ct.SoHoaDon = objHD.SoHoaDon;
                     _ct.SoDonDatHang = item.SoDonDatHang;
                     model.P_HoaDonCT.Add(_ct);
                     model.Entry(item).State = System.Data.Entity.EntityState.Modified;
                 }
                 model.SaveChanges();
                 _trans.Commit();
             }
             catch (Exception ex)
             {
                 _trans.Rollback();
                 throw new Exception(ex.Message);
             }
         }
     }
 }
예제 #2
0
        public static void update(P_HoaDon objHD, List<P_DonDatHang> lstDDH)
        {
            using (var model = ModelConnector.Connector())
            {
                using (var _trans = model.Database.BeginTransaction())
                {
                    try
                    {
                        model.Entry(objHD).State = System.Data.Entity.EntityState.Modified;

                        var lst_HD_CT = model.P_HoaDonCT.Where(a => a.ID_HoaDon == objHD.ID_HoaDon_CT).ToList();
                        foreach (var item in lst_HD_CT)
                        {
                            model.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                            var objDDH = lstDDH.Where(a => a.SoDonDatHang == item.SoDonDatHang).FirstOrDefault();
                            if(objDDH==null)
                            {
                                objDDH = model.P_DonDatHang.Where(a => a.SoDonDatHang == item.SoDonDatHang).FirstOrDefault();
                                objDDH.SoHoaDon = "";
                                model.Entry(objDDH).State = System.Data.Entity.EntityState.Modified;
                            }
                        }
                        foreach (var item in lstDDH)
                        {
                            P_HoaDonCT _ct = new P_HoaDonCT();
                            _ct.ID_HoaDon = objHD.ID_HoaDon_CT;
                            _ct.SoHoaDon = objHD.SoHoaDon;
                            _ct.SoDonDatHang = item.SoDonDatHang;
                            model.P_HoaDonCT.Add(_ct);
                            model.Entry(item).State = System.Data.Entity.EntityState.Modified;
                        }

                        model.SaveChanges();
                        _trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        _trans.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
        }