I am using a WPF window and trying to save an item into the database. I am working with three mysql database tables: Orders, Items, and Meals.
Items are related to both Meals and Orders. Orders table has an OrderID field, and Meals table has MealID. Items table has OrderID and MealID. I am trying to add meals to an order by adding a MealID and an OrderID into the Items table.
So far I had no problem adding entries into the other tables, but when I started trying to add an entry into a table that requires references from two other tables I got stuck. I am reading the MealID from a selected row in the grid that displays the Meals table. The OrderID can also be read from the order datagrid. The database db is imported as an EDO model. This is my code :
Meal selectedMeal = mealsDataGrid.SelectedItem as Meal; // select the meal from the grid
Order order = ordersDataGrid.SelectedItem as Order; // select the order from the grid
Item item = new Item(); // create new item
item.MealReference.Value = selectedMeal; // set the reference to the Meals table
item.OrderReference.Value = order; //set the reference to the Order table
db.AddItems(item); // insert into the Items table
db.SaveChanges(); // save to the database
When I run this I get an exception indicating that I am using the wrong mysql code. I also tried to create the items object hardcoding the values as in Item item = Item.CreateItem(1, 2) using the existing values for OrderId and MealID. Nothing seemed to work. (Similar code works fine with other tables that don't have two dependencies). If anyone has an experience with a similar setup please tell me what I am missing.