public JsonResult addProduct(Product addProduct, NutritonalValue_for_product[] nutritionals) { using (recipes = new recipeEntities()) { try { recipes.SaveChanges(); } catch (DbEntityValidationException ex) { catch_errors(ex); } product p = addProduct.getEntity(); var pr = recipes.Entry(p); pr.State = EntityState.Added; List<products_in_nutritional_value> nutritionalsVal = nutritionals.Select(n => n.getEntity()).ToList(); pr.Entity.products_in_nutritional_value = nutritionalsVal; try { recipes.SaveChanges(); } catch (DbEntityValidationException ex) { catch_errors(ex); } return Json(new { success = true, p = p.getSerialize() }); } }
//=========== create new product from class Product from application classes public static product create(Product product) { using(var res = new recipeEntities()){ product p = new product(); List<int> managers = res.users.Where(u => u.user_or_manager == true) .Select(u => u.id).ToList(); p.name = product.name; p.description = product.description; p.user_id = product.userId; p.approved = managers.Any(id => id == p.user_id); p.amount_weight_in_volume = product.weight_in_volume; p.nutritional_per = product.nutritional_per; return p; } }