public ActionResult CreateProduct(string productName) { string errMsg = "添加成功"; if (string.IsNullOrEmpty(productName)) { errMsg = "产品名称不能为空"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } //开启EF事务 using (TransactionScope ts = new TransactionScope()) { try { IBase_ProductInfoService base_ProductInfoService = BLLContainer.Resolve <IBase_ProductInfoService>(); if (base_ProductInfoService.GetCount(p => p.ProductName.Equals(productName, StringComparison.OrdinalIgnoreCase)) > 0) { errMsg = "该产品名称已存在"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } var base_ProductInfo = new Base_ProductInfo(); base_ProductInfo.ProductName = productName; if (!base_ProductInfoService.Add(base_ProductInfo)) { errMsg = "数据库更新失败"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } var dbContext = DbContextFactory.CreateByModelNamespace(typeof(Base_ProductInfo).Namespace); var userLvls = DictSystemBusiness.GetDicts("客户等级"); foreach (var model in userLvls) { var base_ProductInfo_Detail = new Base_ProductInfo_Detail(); base_ProductInfo_Detail.UserLvl = Convert.ToInt32(model.DictValue); base_ProductInfo_Detail.ProductType = base_ProductInfo.ID; dbContext.Entry(base_ProductInfo_Detail).State = EntityState.Added; } if (dbContext.SaveChanges() < 0) { errMsg = "数据库更新失败"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } ts.Complete(); } catch (Exception ex) { return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = ex.Message })); } } //更新缓存 ProductTypeBusiness.Init(); return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); }
public static bool IsEffective(string productName, out Base_ProductInfo base_ProductInfo) { base_ProductInfo = null; if (ProductInfoList.Any(p => p.ProductName.Equals(productName, StringComparison.OrdinalIgnoreCase))) { base_ProductInfo = ProductInfoList.Where(p => p.ProductName.Equals(productName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); return(true); } return(false); }
public static bool IsEffective(int id, out Base_ProductInfo base_ProductInfo) { base_ProductInfo = null; if (ProductInfoList.Any(p => p.ID == id)) { base_ProductInfo = ProductInfoList.Where(p => p.ID == id).FirstOrDefault(); return(true); } return(false); }