/// <summary> /// 将修改后的业务模型保存到数据库表,并返回业务模型的ID /// </summary> /// <param name="t"></param> /// <returns></returns> public virtual int Change(TModel t) { TEntity data = _dataMapper.ToEntity(t); _innerDataProvider.BeginTrans(); ProcessCollections(t, data); try { var r = _innerDataProvider.Change(data); ProcessAttachmentCollections(t, data); _dataMapper.SaveLanguages(_innerDataProvider, data, t); _innerDataProvider.EndTrans(); return(data.Id); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { var errMsg = String.Join(";", ex.EntityValidationErrors.Select(val => String.Join(",", val.ValidationErrors.Select(v => v.ErrorMessage)))); _innerDataProvider.RollbackTrans(); throw new JException(errMsg); } catch { _innerDataProvider.RollbackTrans(); throw; } }
/// <summary> /// 保存岗位数据 /// </summary> /// <param name="postModelList"></param> /// <returns></returns> public bool SavePostInfo(List <PostModel> postModelList) { var _context = SiteManager.Kernel.Get <ModelContext>(); EFAuditDataService <PostModel> efMain = new EFAuditDataService <PostModel>(_context); try { int row = 0; #region 岗位信息 foreach (PostModel item in postModelList) { if (item._State.ToUpper() == "ADDED") { row = efMain.Add(item); } else if (item._State.ToUpper() == "MODIFIED") { row = efMain.Change(item); } else if (item._State.ToUpper() == "REMOVED") { row = efMain.Delete(item); } if (row < 0) { efMain.RollbackTrans(); return(false); } } #endregion } catch (Exception) { efMain.RollbackTrans(); throw; } efMain.EndTrans(); return(true); }
/// <summary> /// 保存 /// </summary> /// <param name="authTokenModel">数据节点对象</param> /// <returns></returns> public bool SaveInfo(DepartmentModel organizationModel) { var _context = SiteManager.Kernel.Get <ModelContext>(); EFAuditDataService <DepartmentModel> efMain = new EFAuditDataService <DepartmentModel>(_context); EFAuditDataService <DepPostModel> efPost = new EFAuditDataService <DepPostModel>(_context); EFAuditDataService <DepUserModel> efUser = new EFAuditDataService <DepUserModel>(_context); try { #region 组织机构主表数据 efMain.BeginTrans(); int row = 0; if (organizationModel.Id == null || organizationModel.Id <= 0) { row = efMain.Add(organizationModel); } else { row = efMain.Change(organizationModel); } if (row < 0) { efMain.RollbackTrans(); return(false); } #endregion #region 组织机构与岗位信息 foreach (DepPostModel item in organizationModel.DepPostModelList) { item.DepId = organizationModel.Id; item.IsActive = 1; item.ExamineType = ""; item.IsDeleted = 0; item.IsDisabled = 0; item.Name = ""; item.CreateDatetime = DateTime.Now; if (item._State.ToUpper() == "ADDED") { row = efPost.Add(item); } else if (item._State.ToUpper() == "MODIFIED") { row = efPost.Change(item); } else if (item._State.ToUpper() == "REMOVED") { row = efPost.Delete(item); } if (row < 0) { efMain.RollbackTrans(); return(false); } } #endregion #region 组织机构与用户信息 foreach (DepUserModel item in organizationModel.DepUserModelList) { #region 设置用户的主部门,清除已存在其他主部门标识,确定一个用户只有一个主要部门 //如果某个用户设置了主部门首先查询是否在其他部门下已经存在设置,如果存在清除掉,重新写入主部门标识 if (item.IsMain == 1) { List <DepUserModel> dList = efUser.GetQuery().Where(u => u.UserName == item.UserName && u.DepId != item.DepId).ToList(); foreach (DepUserModel m in dList) { m.IsMain = 0; row = efUser.Change(m); if (row < 0) { efMain.RollbackTrans(); return(false); } } } #endregion item.DepId = organizationModel.Id; item.ContractLenght = item.ContractLenght == null ? 0 : item.ContractLenght; item.ExamineType = ""; item.IsDeleted = 0; item.JoinDateTime = item.JoinDateTime == null ? DateTime.Now : item.JoinDateTime; item.IsSuspension = item.IsSuspension == null ? 0 : item.ContractLenght; item.IsLeader = item.IsLeader == null ? 0 : item.IsLeader; item.IsMain = item.IsMain == null ? 0 : item.IsMain; item.CreateDatetime = DateTime.Now; if (item._State.ToUpper() == "ADDED") { row = efUser.Add(item); } else if (item._State.ToUpper() == "MODIFIED") { row = efUser.Change(item); } else if (item._State.ToUpper() == "REMOVED") { row = efUser.Delete(item); } if (row < 0) { efMain.RollbackTrans(); return(false); } } #endregion } catch (Exception) { efMain.RollbackTrans(); throw; } efMain.EndTrans(); return(true); }
public void EFTestMultiObjectAddOrChange() { var context = new TestContext(); Random rand = new Random(100); /////////////////////////////添加供应商/////////////////////////////////////// List <Supplier> suppliers = new List <Supplier>(); string prefix = DateTime.Now.ToString("yyMMddHHmmss"); for (int i = 0; i < 100; i++) { suppliers.Add(new Supplier { Name = "Supplier" + prefix + i, Address = "Address" + prefix + i, Tel = "12345" + prefix + i, }); } EFAuditDataService <Supplier> supplierProvider = new EFAuditDataService <Supplier>(context); var count = supplierProvider.Add(suppliers); Assert.AreEqual(count, suppliers.Count); /////////////////////////////添加产品/////////////////////////////////////// List <Product> products = new List <Product>(); for (int i = 0; i < 1000; i++) { products.Add(new Product { Name = "Product" + prefix + i, Model = "Model" + prefix + i, Price = DateTime.Now.Millisecond, Unit = DateTime.Now.Second, }); } EFAuditDataService <Product> productProvider = new EFAuditDataService <Product>(context); count = productProvider.Add(products); Assert.AreEqual(count, products.Count); /////////////////////////////添加服务/////////////////////////////////////// List <Service> services = new List <Service>(); for (int i = 0; i < 200; i++) { services.Add(new Service { Name = "Service" + prefix + i, ServiceType = "Type" + prefix + i, Price = DateTime.Now.Millisecond, }); } EFAuditDataService <Service> serviceProvider = new EFAuditDataService <Service>(context); count = serviceProvider.Add(services); Assert.AreEqual(count, services.Count); services.ForEach(svc => svc.Price *= 1.2m); //测试批量修改 count = serviceProvider.Change(services); Assert.AreEqual(count, services.Count); /////////////////////////////添加订单/////////////////////////////////////// List <Order> orders = new List <Order>(); for (int i = 0; i < 50; i++) { var order = new Order { Name = "Order" + prefix + i, SupplierId = suppliers[rand.Next(suppliers.Count)].Id, }; order.Details = new List <OrderDetail>(); for (int j = 0; j < rand.Next(10); j++) { order.Details.Add(new OrderDetail { Order = order, Product = products[rand.Next(products.Count)], Quantity = rand.Next(10), }); } order.Amount = order.Details.Sum(detail => detail.Quantity * detail.Product.Price); orders.Add(order); } EFAuditDataService <Order> orderProvider = new EFAuditDataService <Order>(context); orderProvider.Add(orders); /////////////////////////////添加服务合同/////////////////////////////////////// List <Contract> contracts = new List <Contract>(); for (int i = 0; i < 20; i++) { var service = services[rand.Next(services.Count)]; contracts.Add(new Contract { Service = service, Name = "ServiceContract" + prefix + i, SupplierId = suppliers[rand.Next(suppliers.Count)].Id, Amount = service.Price * 0.9m, }); } EFAuditDataService <Contract> contractProvider = new EFAuditDataService <Contract>(context); count = contractProvider.Add(contracts); Assert.AreEqual(count, contracts.Count); }