Hi! I am developing a C # application for myself using the Entity Framework and I started to get to add and display data from the database, but I need to do so that in the RecipeIngredient table immediately after adding the recipe, I added the ingredients to it as Id of this ingredient. Here so the model of my DB now looks.
Products that are needed for the recipe I store in a List array. List<string> productsAddList = new List<string>(); It seems to me when adding it will be possible to iterate through it in a loop and match the data in the RecipeIngredient.RecipeIngredient I created because I will need to do a search for recipes by products later.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class PlusRecepies : Form { int x = 0; List<string> productsAddList = new List<string>(); public PlusRecepies() { InitializeComponent(); } private void PlusToFlp_Click(object sender, EventArgs e) { var label1 = new Label(); flowLayoutPanel2.Controls.Add(label1); label1.Text = ProductPlus.Text; productsAddList.Add(ProductPlus.Text); } private void AddRecepieToDB_Click(object sender, EventArgs e) { Db1Context context = new Db1Context(); Recipe recipe = new Recipe { Instructions = RecepiePRText.Text, Name = NameRecipe.Text, Prop_Time = Convert.ToInt32(PropTime.Text) }; for (int i = 0; i < productsAddList.Count; i++) { Ingredients ingredient = new Ingredients { Name = productsAddList[i] }; context.Ingredients.Add(ingredient); recipe.RecipeIngredient.Add(ingredient); } context.Recipe.Add(recipe); context.SaveChanges(); } } } Ingredients.cs
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace WindowsFormsApp1 { using System; using System.Collections.Generic; public partial class Ingredients { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Ingredients() { this.RecipeIngredient = new HashSet<RecipeIngredient>(); } public int Id { get; set; } public string Name { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<RecipeIngredient> RecipeIngredient { get; set; } } } Recipe.cs
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace WindowsFormsApp1 { using System; using System.Collections.Generic; public partial class Recipe { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Recipe() { this.RecipeIngredient = new HashSet<RecipeIngredient>(); } public int Id { get; set; } public string Name { get; set; } public Nullable<int> Prop_Time { get; set; } public string Instructions { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<RecipeIngredient> RecipeIngredient { get; set; } } } RecipeIngredients.cs
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace WindowsFormsApp1 { using System; using System.Collections.Generic; public partial class RecipeIngredient { public int Id { get; set; } public int RecipeId { get; set; } public int IngredientId { get; set; } public virtual Ingredients Ingredients { get; set; } public virtual Recipe Recipe { get; set; } } } 
recipe.RecipeIngredient.Add(ingredient). Shl. it is better to use the CodeFirst approach and think in its terms, then you will not have such questions, I think - Andrew NOP