Inserting and Deleting a many-to-many relationship with the Entity Framework (C#)
(Originally coded in VS2008 using c#, .net framework 3.5, compatibility with VS2005/.net 2.0 unknown)
In my last post, I showed you how to use an entity datasource to query many-to-many relationships and display them on a gridview.
Now, we’ll go over the same scenario, but we’ll be using our gridviews to insert/delete records in our bridge table.
For review, we’re dealing with a database structure that looks like this:

This is how a many to many looks in your database, but not in your edmx
Conceptually, it looks like the below (the bridge table is represented as a many-to-many ( *:* ) relationship between our two entities:

This is how a many-to-many relationship looks in your data model
And once we’ve bound our gridviews (as in the last article), we should be looking at something like this:

the menu and the pantry
I’m going to assume a general understanding of gridview rowcommand events, but if anyone needs further explanation, feel free to ask.
The first action we’ll look at is the insert. As you can see in the screenshot above, the user can click the arrow on the pantry to add a given item to the current menu. The code will look like this for an insert:
protected void gvPantry_RowCommand(object sender, GridViewCommandEventArgs e) { /************************************************************************** * this function handles the row commands on the pantry (adds item to menu) * ************************************************************************/ //"if" required to avoid raising this on ddlPantyFilter postback if (e.CommandName.ToString() == "AddItemToMenu") { Int32 mealID = Convert.ToInt32(((ImageButton)gvMeals.SelectedRow. _ FindControl("btnSelectMeal")).CommandArgument); Int32 pantryItemID = Convert.ToInt32(e.CommandArgument); using (BusLineEntities myEnts = new BusLineEntities()) { Meal thisMeal = myEnts.Meals.FirstOrDefault(m => m.Id == mealID); PantryItem thisPI = myEnts.PantryItems. _ FirstOrDefault(p => p.Id == pantryItemID); thisMeal.PantryItems.Add(thisPI); myEnts.SaveChanges(); } gvFoodOnMenu.DataBind(); gvPantry.DataBind(); } } //end void gvPantry_RowCommand()
Fairly straightforward, but now let’s say you need to do a delete. It’s not much different, really, and the code will look something like this:
protected void gvFoodOnMenu_RowCommand(object sender, GridViewCommandEventArgs e) { /**************************************************************************** * this function handles the row commands on the menu (removes item from menu) * **************************************************************************/ Int32 mealID = Convert.ToInt32(((ImageButton)gvMeals.SelectedRow. _ FindControl("btnSelectMeal")).CommandArgument); Int32 pantryItemID = Convert.ToInt32(e.CommandArgument); using (BusLineEntities myEnts = new BusLineEntities()) { Meal thisMeal = myEnts.Meals.FirstOrDefault(m => m.Id == mealID); PantryItem thisPI = myEnts.PantryItems. _ FirstOrDefault(p => p.Id == pantryItemID); thisMeal.PantryItems.Attach(thisPI); thisMeal.PantryItems.Remove(thisPI); myEnts.SaveChanges(); } gvFoodOnMenu.DataBind(); gvPantry.DataBind(); } //end void gvPantry_RowCommand()








[...] how to query a many-to-many relationship using an entityDataSource for display on a gridview. The next article will show you how to insert/delete, using the same example we’re working with [...]
Leave your response!