コード例 #1
0
        public StoreItemBrand AddBrand(StoreItemBrand brand)
        {
            try
            {
                using (var db = new ShopKeeperStoreEntities(_connectionString))
                {
                    StoreItemBrand brandInfo;
                    var            brands = db.StoreItemBrands.Where(b => b.Name.Trim().ToLower().Replace(" ", "") == brand.Name.Trim().ToLower().Replace(" ", "")).ToList();
                    if (!brands.Any())
                    {
                        brand.LastUpdated = DateTime.Now;
                        var processedBrand = db.StoreItemBrands.Add(brand);
                        db.SaveChanges();
                        brandInfo = processedBrand;
                    }
                    else
                    {
                        brandInfo = brands[0];
                    }
                    return(brandInfo);
                }
            }
            catch (DbEntityValidationException e)
            {
                var str = "";
                foreach (var eve in e.EntityValidationErrors)
                {
                    str += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                         eve.Entry.Entity.GetType().Name, eve.Entry.State) + "\n";
                    foreach (var ve in eve.ValidationErrors)
                    {
                        str += string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                             ve.PropertyName, ve.ErrorMessage) + " \n";
                    }
                }

                ErrorLogger.LogError(e.StackTrace, e.Source, str);
                return(new StoreItemBrand());
            }
        }
コード例 #2
0
        private long ProcessRecord(DataRowView dv, ref string msg)
        {
            if (dv == null)
            {
                return(0);
            }
            try
            {
                using (var db = _dbStoreEntities)
                {
                    var mInfo = new StoreItem
                    {
                        Name = dv.Row["Name"].ToString().Trim()
                    };
                    var duplicates = db.StoreItems.Count(m => m.Name.ToLower().Trim() == mInfo.Name.ToLower().Trim());
                    if (duplicates > 0)
                    {
                        return(0);
                    }
                    var parentProductStr = dv.Row["Parent_Product_Name"].ToString().Trim();
                    if (!string.IsNullOrEmpty(parentProductStr))
                    {
                        var parentItems = db.StoreItems.Where(m => m.Name.ToLower().Trim() == parentProductStr.ToLower().Trim()).ToList();
                        if (parentItems.Any())
                        {
                            var parentItemId = parentItems[0].StoreItemId;
                            if (parentItemId > 0)
                            {
                                mInfo.ParentItemId = parentItemId;
                            }
                        }
                    }

                    var productTypeStr = dv.Row["Product_Type"].ToString().Trim();

                    if (!string.IsNullOrEmpty(productTypeStr))
                    {
                        var productTypes = db.StoreItemTypes.Where(m => m.Name.ToLower().Trim() == productTypeStr.ToLower().Trim()).ToList();
                        if (productTypes.Any())
                        {
                            var productType = productTypes[0];
                            if (productType != null && productType.StoreItemTypeId > 0)
                            {
                                mInfo.StoreItemTypeId = productType.StoreItemTypeId;
                            }
                        }
                        else
                        {
                            var productType = new StoreItemType {
                                Name = productTypeStr.Trim()
                            };
                            var processedProductType = db.StoreItemTypes.Add(productType);
                            db.SaveChanges();
                            if (processedProductType.StoreItemTypeId > 0)
                            {
                                mInfo.StoreItemTypeId = processedProductType.StoreItemTypeId;
                            }
                            else
                            {
                                msg = "Product Type Information could not be added.";
                                return(0);
                            }
                        }
                    }
                    else
                    {
                        msg = "Product Type is empty.";
                        return(0);
                    }

                    var productBrandStr = dv.Row["Product_Brand"].ToString().Trim();

                    if (!string.IsNullOrEmpty(productBrandStr))
                    {
                        var productTypes = db.StoreItemTypes.Where(m => m.Name.ToLower().Trim() == productBrandStr.ToLower().Trim()).ToList();
                        if (productTypes.Any())
                        {
                            var productType = productTypes[0];
                            if (productType != null && productType.StoreItemTypeId > 0)
                            {
                                mInfo.StoreItemTypeId = productType.StoreItemTypeId;
                            }
                        }
                        else
                        {
                            var productBrand = new StoreItemBrand {
                                Name = productBrandStr.Trim()
                            };
                            var processedProductBrand = db.StoreItemBrands.Add(productBrand);
                            db.SaveChanges();
                            if (processedProductBrand.StoreItemBrandId > 0)
                            {
                                mInfo.StoreItemBrandId = processedProductBrand.StoreItemBrandId;
                            }
                            else
                            {
                                msg = "Product Brand Information could not be added.";
                                return(0);
                            }
                        }
                    }
                    else
                    {
                        msg = "Product Brand is empty.";
                        return(0);
                    }

                    var productCategoryStr = dv.Row["Product_Category"].ToString().Trim();

                    if (!string.IsNullOrEmpty(productCategoryStr))
                    {
                        var productCategories = db.StoreItemCategories.Where(m => m.Name.ToLower().Trim() == productCategoryStr.ToLower().Trim()).ToList();
                        if (productCategories.Any())
                        {
                            var productCategory = productCategories[0];
                            if (productCategory != null && productCategory.StoreItemCategoryId > 0)
                            {
                                mInfo.StoreItemTypeId = productCategory.StoreItemCategoryId;
                            }
                        }
                        else
                        {
                            var productCategory = new StoreItemCategory {
                                Name = productCategoryStr.Trim()
                            };
                            var processedProductCategory = db.StoreItemCategories.Add(productCategory);
                            db.SaveChanges();
                            if (processedProductCategory.StoreItemCategoryId > 0)
                            {
                                mInfo.StoreItemCategoryId = processedProductCategory.StoreItemCategoryId;
                            }
                            else
                            {
                                msg = "Product Category is empty.";
                                return(0);
                            }
                        }
                    }
                    else
                    {
                        msg = "Product Category is empty.";
                        return(0);
                    }

                    var processedProduct = db.StoreItems.Add(mInfo);
                    db.SaveChanges();
                    return(processedProduct.StoreItemId);
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex.StackTrace, ex.Source, ex.Message);
                return(0);
            }
        }