private OPResult Update(SysOrganizationBO organization) { var brandIDs = CurrentOrganization.Brands.Select(o => o.ID); List <OrganizationBrand> obs = new List <OrganizationBrand>(); foreach (var b in organization.Brands) { OrganizationBrand ob = new OrganizationBrand { OrganizationID = organization.ID, BrandID = b.ID, CreatorID = VMGlobal.CurrentUser.ID, CreateTime = DateTime.Now }; obs.Add(ob); } OPResult result = null; using (TransactionScope scope = new TransactionScope()) { try { LinqOP.Update <SysOrganization>(organization); VMGlobal.SysProcessQuery.LinqOP.Delete <OrganizationBrand>(ob => brandIDs.Contains(ob.BrandID) && ob.OrganizationID == organization.ID); VMGlobal.SysProcessQuery.LinqOP.Add <OrganizationBrand>(obs); scope.Complete(); result = new OPResult { IsSucceed = true, Message = "更新成功!" }; } catch (Exception e) { result = new OPResult { IsSucceed = false, Message = "更新失败,失败原因:\n" + e.Message }; } } if (result.IsSucceed) { int index = VMGlobal.ChildOrganizations.FindIndex(o => o.ID == organization.ID); if (organization.Flag) { if (index == -1) { VMGlobal.ChildOrganizations.Add(organization); } else { VMGlobal.ChildOrganizations[index] = organization; } } else if (index != -1) { VMGlobal.ChildOrganizations.RemoveAt(index); } _currentAndChildrenOrganizations = null; } return(result); }
private OPResult Add(SysOrganizationBO organization) { organization.CreatorID = VMGlobal.CurrentUser.ID; organization.ParentID = VMGlobal.CurrentUser.OrganizationID; using (TransactionScope scope = new TransactionScope()) { try { int id = LinqOP.Add <SysOrganization, int>(organization, o => o.ID); organization.ID = id;//将ID赋值,表明该对象不再是新增对象(新增对象ID都为0) List <OrganizationBrand> obs = new List <OrganizationBrand>(); foreach (var b in organization.Brands) { OrganizationBrand ob = new OrganizationBrand { OrganizationID = id, BrandID = b.ID, CreatorID = organization.CreatorID, CreateTime = DateTime.Now }; obs.Add(ob); } VMGlobal.SysProcessQuery.LinqOP.Add <OrganizationBrand>(obs); //同时建立仓库,由于存在跨数据库操作,需要在数据库服务器上启动DTC服务 //后注:虽然在服务器上启动了DTC并按网上所述全部配置完毕,但却抛出“MSDTC被禁用”的异常。 //OrganizationLogic.AddDefaultStorage(organization); scope.Complete(); } catch (Exception e) { organization.ID = default(int); return(new OPResult { IsSucceed = false, Message = "保存失败,失败原因:\n" + e.Message }); } } if (!VMGlobal.ChildOrganizations.Any(o => o.ID == organization.ID)) { VMGlobal.ChildOrganizations.Add(organization); } _currentAndChildrenOrganizations = null; //OrganizationLogic.AddDefaultStorage(organization);这里去除新建默认仓库的逻辑 return(new OPResult { IsSucceed = true, Message = "保存成功!" }); }
public override OPResult AddOrUpdate(SysOrganization entity) { SysOrganizationBO bo = (SysOrganizationBO)entity; return(bo.ID == default(int) ? this.Add(bo) : this.Update(bo)); }