//---------------保存CRMCategory---------------------------
        public CRMCategory Save(CRMCategory entity, IList<CRMProduct> prodList)
        {
            if (this.dataCtx.Connection != null)
                if (this.dataCtx.Connection.State == ConnectionState.Closed)
                    this.dataCtx.Connection.Open();
            DbTransaction tran = this.dataCtx.Connection.BeginTransaction();
            dataCtx.Transaction = tran;

            try
            {
                var qry = from t in CRMCategorys
                          where t.CatID == entity.CatID
                          select t;
                var obj = qry.SingleOrDefault();
                if (obj != null)
                    this.CopyEntity(obj, entity);
                else
                    this.CRMCategorys.InsertOnSubmit(entity);

                this.dataCtx.SubmitChanges();

                //delete  relationship with product
                var qryDel = from t in CRMCategoryProds
                             where t.CatID == entity.CatID
                             select t;
                foreach (var item in qryDel.ToList())
                {
                    this.CRMCategoryProds.DeleteOnSubmit(item);
                }
                //add new
                foreach (var prod in prodList)
                {
                        var p = new CRMCategoryProd();
                        p.CatID = entity.CatID;
                        p.ProdCode = prod.Code;
                        this.CRMCategoryProds.InsertOnSubmit(p);
                }

                this.dataCtx.SubmitChanges();

                tran.Commit();
                return entity;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw ex;
            }
            finally
            {
                dataCtx.Connection.Close();
            }
        }
Exemple #2
0
        //---------------保存CRMCategory---------------------------
        public CRMCategory Save(CRMCategory entity, IList <CRMProduct> prodList)
        {
            if (this.dataCtx.Connection != null)
            {
                if (this.dataCtx.Connection.State == ConnectionState.Closed)
                {
                    this.dataCtx.Connection.Open();
                }
            }
            DbTransaction tran = this.dataCtx.Connection.BeginTransaction();

            dataCtx.Transaction = tran;

            try
            {
                var qry = from t in CRMCategorys
                          where t.CatID == entity.CatID
                          select t;
                var obj = qry.SingleOrDefault();
                if (obj != null)
                {
                    this.CopyEntity(obj, entity);
                }
                else
                {
                    this.CRMCategorys.InsertOnSubmit(entity);
                }

                this.dataCtx.SubmitChanges();

                //delete  relationship with product
                var qryDel = from t in CRMCategoryProds
                             where t.CatID == entity.CatID
                             select t;
                foreach (var item in qryDel.ToList())
                {
                    this.CRMCategoryProds.DeleteOnSubmit(item);
                }
                //add new
                foreach (var prod in prodList)
                {
                    var p = new CRMCategoryProd();
                    p.CatID    = entity.CatID;
                    p.ProdCode = prod.Code;
                    this.CRMCategoryProds.InsertOnSubmit(p);
                }

                this.dataCtx.SubmitChanges();


                tran.Commit();
                return(entity);
            }
            catch (Exception ex)
            {
                tran.Rollback();
                throw ex;
            }
            finally
            {
                dataCtx.Connection.Close();
            }
        }