C# 如何在实体框架中更新相关表?

C# 如何在实体框架中更新相关表?,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我有一个名为Tour的表和另一个名为TourPlan的表。 这些表格如下所示 Tour TourPlan Id (PK,int,not null) Id (PK, int, not null) Title (nvarchar(100), null) Title (nvarchar(100), null) TourId (FK, int, not nu

我有一个名为
Tour
的表和另一个名为
TourPlan
的表。 这些表格如下所示

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)
问题是使用现有值更新
TourPlan

这是我更新的代码

Tour tour = TourController.GetTourById(Int32.Parse("13"));
tour.TourPlan.Clear();
foreach (ListItem item in lbPlans.Items)
   {
     tour.TourPlan.Add(new TourPlan() { Title = item.Text });
   }
这是我的更新方法

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();
     }
 }
但它没有更新,它插入了两次计划。
我怎样才能解决这个问题

您需要更新TourPlan的数据,而不是覆盖实例:

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();
     }
 }

当然,这假设您已经拥有TourPlan的附加实例。如果没有,则需要将其附加到DbContext。

您需要更新TourPlan的数据,而不是覆盖实例:

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();
     }
 }

当然,这假设您已经拥有TourPlan的附加实例。如果没有,则需要将其附加到DbContext。

更新TourPlan的方法如下所示:

    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();
        }
    }

方法的第二个参数必须已经“准备好”
TourPlan

这是更新TourPlan的方法的样子:

    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();
        }
    }

方法的第二个参数必须已经“准备好”
TourPlan

Tour有多个TourPlan,因此两个表之间存在1对n的关系。那么Tour.TourPlan是一个集合?在这种情况下,您应该在集合上循环并复制值。是。这是收藏。但若用户删除了一个计划并在更新中添加了另一个计划,那个么该如何实现呢?您需要将其与DbContext同步。为此,您需要将其附加到上下文。您可以查看本文了解更多信息:Tour有多个TourPlan,因此两个表之间存在1到n的关系。Tour.TourPlan是一个集合吗?在这种情况下,您应该在集合上循环并复制值。是。这是收藏。但若用户删除了一个计划并在更新中添加了另一个计划,那个么该如何实现呢?您需要将其与DbContext同步。为此,您需要将其附加到上下文。您可以查看本文以了解更多信息: