// work out if the Division already exists, if it does then use it for the mapping table, else create it private void addDivisions(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { // clear the current mappings var maps = context.RecipeDivisionMaps.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeDivisionMaps.DeleteAllOnSubmit(maps); context.SubmitChanges(); if (recipe.ValidDivisions == null) { return; } foreach (var division in recipe.ValidDivisions) { // create or retrieve the RecipeCuisine.ID value var master = context.RecipeDivisions.SingleOrDefault(c => c.Description == division.ValidDivision); var recipeDivisionID = 0; if (master == null) { var toCreate = new RecipeDivision() { Description = division.ValidDivision }; context.RecipeDivisions.InsertOnSubmit(toCreate); context.SubmitChanges(); recipeDivisionID = toCreate.ID; } else { recipeDivisionID = master.ID; } // add to the mapping table var map = new RecipeDivisionMap() { RecipeDivisionID = recipeDivisionID, RecipeID = recipe.RecipeID }; context.RecipeDivisionMaps.InsertOnSubmit(map); context.SubmitChanges(); } } }
// work out if the Division already exists, if it does then use it for the mapping table, else create it private void addDivisions(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { // clear the current mappings var maps = context.RecipeDivisionMaps.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeDivisionMaps.DeleteAllOnSubmit(maps); context.SubmitChanges(); if (recipe.ValidDivisions == null) return; foreach (var division in recipe.ValidDivisions) { // create or retrieve the RecipeCuisine.ID value var master = context.RecipeDivisions.SingleOrDefault(c => c.Description == division.ValidDivision); var recipeDivisionID = 0; if (master == null) { var toCreate = new RecipeDivision() { Description = division.ValidDivision }; context.RecipeDivisions.InsertOnSubmit(toCreate); context.SubmitChanges(); recipeDivisionID = toCreate.ID; } else { recipeDivisionID = master.ID; } // add to the mapping table var map = new RecipeDivisionMap() { RecipeDivisionID = recipeDivisionID, RecipeID = recipe.RecipeID }; context.RecipeDivisionMaps.InsertOnSubmit(map); context.SubmitChanges(); } } }