1

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?

2 Answers 2

3

You would need to update the data of TourPlan instead of overwriting the instance:

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.Id= tour.TourPlan.Id;
                tUpd.TourPlan.TourId= tour.TourPlan.TourId;
                tUpd.TourPlan.Title = tour.TourPlan.Title;
            }
          return context.SaveChanges();
     }
 }

This of course supposes that you already have an attached instance of TourPlan. If not, you need to attach it to the DbContext.

Sign up to request clarification or add additional context in comments.

4 Comments

Tour has multiple TourPlan so there is 1 to n relationship between two tables.
so Tour.TourPlan is a collection? In that case you should loop over the collection and copy the values.
Yes. It'is collection. But if user deletes a plan and adds another plan in update, how would this be achieved?
You would need to synchronize it with the DbContext. To do this you need to attach it to the context. You can check this article for more information: msdn.microsoft.com/en-us/data/jj592676.aspx
1

This is how your method for updating TourPlan should look like:

    public static void UpdateTour(Tour tour, TourPlan tourPlan)
    {
        using (var context = new aisatourismEntities())
        {
            context.Tours.Attach(tour);

            context.Entity(tourPlan).Property(plan => plan.Title).IsModified = true;

            context.SaveChanges();
        }
    }

The second parameter of the method has to be already "prepared" TourPlan.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.