Example #1
0
        private void InsertExistingBarCombinations()
        {
            if (ExistingBarCombinationManager.Get(CurrentContextType).Count() != 0)
            {
                throw new Exception("Combinations already inserted !");
            }

            string filename     = System.Configuration.ConfigurationManager.AppSettings["DelistedList"];
            string fullFilename = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/" + filename);

            try
            {
                string[] fileLines = System.IO.File.ReadAllLines(fullFilename);
                foreach (string s in fileLines)
                {
                    string[] ingredientData            = s.Split(new char[] { ',' });
                    ExistingBarCombination existingBar = new ExistingBarCombination();
                    existingBar.BarName     = ingredientData[0];
                    existingBar.Ingredient1 = IngredientManager.GetIngredient(ingredientData[1], CurrentContextType);
                    existingBar.CreatedOn   = DateTime.UtcNow;
                    if (!string.IsNullOrEmpty(ingredientData[2]))
                    {
                        existingBar.Ingredient2 = IngredientManager.GetIngredient(ingredientData[2], CurrentContextType);
                    }

                    if (!string.IsNullOrEmpty(ingredientData[3]))
                    {
                        existingBar.Ingredient3 = IngredientManager.GetIngredient(ingredientData[3], CurrentContextType);
                    }
                    ExistingBarCombinationManager.Insert(existingBar, CurrentContextType);
                }
            }
            catch (System.IO.FileNotFoundException ioExcp)
            {
                throw new Exception("Ingredients file could not be loaded.", ioExcp);
            }
            catch (System.IO.IOException ioGeneralExcp)
            {
                throw new System.IO.IOException("An IO Exception occurred while reading the ingredients file.", ioGeneralExcp);
            }
            catch (Exception excp)
            {
                throw new Exception("A general exception occurred while reading the ingredients file.", excp);
            }
        }
        public static void Insert(ExistingBarCombination existingBarCombination, ContextType contextType = ContextType.Default)
        {
            using (var context = ContextSwitcher.CreateContext(contextType))
            {
                if (existingBarCombination.Ingredient1 != null)
                {
                    context.Ingredients.Attach(existingBarCombination.Ingredient1);
                }
                if (existingBarCombination.Ingredient2 != null)
                {
                    context.Ingredients.Attach(existingBarCombination.Ingredient2);
                }
                if (existingBarCombination.Ingredient3 != null)
                {
                    context.Ingredients.Attach(existingBarCombination.Ingredient3);
                }

                context.ExistingBarCombinations.Add(existingBarCombination);

                context.SaveChanges();
            }
        }