I have a table named Tour and another table name TourPlan.
These tables are related like below;
Tour TourPlan
Id (PK,int,not null) Id (PK, int, not null)
Title (nvarchar(100), null) Title (nvarchar(100), null)
TourId (FK, int, not null)
The problem is to update TourPlan table with existing values.
This is my code for updating;
Tour tour = TourController.GetTourById(Int32.Parse("13"));
tour.TourPlan.Clear();
foreach (ListItem item in lbPlans.Items)
{
tour.TourPlan.Add(new TourPlan() { Title = item.Text });
}
And this is my update method;
public static int UpdateTour(Tour tour)
{
using (var context = new aisatourismEntities())
{
Tour tUpd = context.Tour.FirstOrDefault(t => t.Id == tour.Id);
if (tUpd != null)
{
tUpd.Title = tour.Title;
tUpd.TourPlan = tour.TourPlan;
}
return context.SaveChanges();
}
}
But it's not updating, it inserts plan twice. How can I solve this?