// work out if the Fact already exists, if it does then use it for the mapping table, else create it private static void addFacts(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { // clear the current mappings var maps = context.RecipeFactMaps.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeFactMaps.DeleteAllOnSubmit(maps); context.SubmitChanges(); if (recipe.Facts == null) { return; } foreach (var factType in recipe.Facts) { // create or retrieve the RecipeCuisine.ID value var master = context.RecipeFacts.SingleOrDefault(c => c.Description == factType); var recipeFactID = 0; if (master == null) { var toCreate = new RecipeFact() { Description = factType }; context.RecipeFacts.InsertOnSubmit(toCreate); context.SubmitChanges(); recipeFactID = toCreate.ID; } else { recipeFactID = master.ID; } // add to the mapping table var map = new RecipeFactMap() { RecipeFactID = recipeFactID, RecipeID = recipe.RecipeID }; context.RecipeFactMaps.InsertOnSubmit(map); context.SubmitChanges(); } } }
// work out if the Fact already exists, if it does then use it for the mapping table, else create it private static void addFacts(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { // clear the current mappings var maps = context.RecipeFactMaps.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeFactMaps.DeleteAllOnSubmit(maps); context.SubmitChanges(); if (recipe.Facts == null) return; foreach (var factType in recipe.Facts) { // create or retrieve the RecipeCuisine.ID value var master = context.RecipeFacts.SingleOrDefault(c => c.Description == factType); var recipeFactID = 0; if (master == null) { var toCreate = new RecipeFact() { Description = factType }; context.RecipeFacts.InsertOnSubmit(toCreate); context.SubmitChanges(); recipeFactID = toCreate.ID; } else { recipeFactID = master.ID; } // add to the mapping table var map = new RecipeFactMap() { RecipeFactID = recipeFactID, RecipeID = recipe.RecipeID }; context.RecipeFactMaps.InsertOnSubmit(map); context.SubmitChanges(); } } }