EF4 POCO在另一个上下文上完成更新时不更新关系。 [英] EF4 POCO not updating relations when an update has been done on another context.

查看:67
本文介绍了EF4 POCO在另一个上下文上完成更新时不更新关系。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对POCO和几个上下文有问题。

I have a problem with POCO and several contexts.

问题设置:

我从一个ObjectContext(ReadContext)加载我的POCO,然后使用导航属性。然后从数据库中获取完整的POCO(SQL CE 3.5 SP2,并使用EFProf监视流量)。然后,我创建一个新的ObjectContext(EditContext)
,并使用相同的UniqueID将相同的POCO提取到新的上下文中。我然后修改POCO上的一些属性并保存(使用EditContext)。更改将持久保存到数据库中。

Problem setup:
I load my POCO from one ObjectContext (ReadContext)and then use the navigation properties. The complete POCO is then fetched from the database (a SQL CE 3.5 SP2 and the traffic is monitored using EFProf). I then create a new ObjectContext (EditContext) and fetch the same POCO, i.e. using the same UniqueID, into the new context. I then modify some properties on the POCO and save it (using the EditContext). The changes are persisted to the database OK.

现在问题。

使用第一个上下文(仍在范围内的ReadContext) 我获取再次POCO,我可以看到(使用EFProf)EF进入数据库并获取数据,即提取新更新的数据 我可以查看更改。
但是当访问导航属性时,不会访问数据库,并且我只获取已存在于上下文中的旧对象,而不是数据库中已修改的对象。我无法理解为什么关系不会从
重新读取数据库。

Now to the problem.
Using the first context (ReadContext which is still in scope) I fetch the POCO again and I can see (using EFProf) that EF goes to the database and fetches the data, i.e. the newly updated data is fetched and I can see the changes. But when accessing the Navigation properties no access to the datebase is made and I only get the old objects that were already in the context and not the modified ones that are in the database. I cant understand why the relations are not reread from the database.

我现在的解决方案是丢弃我的ReadContext并再次创建它。这样做的缺点是必须重新加载所有POCO,尽管只有少数POCO被更改。重新加载需要时间......

My solution now is to throw away my ReadContext and create it again. The drawback with this is that all POCOs must be reloaded although only a few of them were changed. And the reloading takes time...

我试图使用MergeOptions但没有区别。

I have tried to use the MergeOptions but there is no difference.

任何id&eac​​ute; as?

Any idéas?

BR,

/ Peter

推荐答案

您需要在两个查询上设置MergeOptions:带回 对象的那个以及带回相关对象的那个。对于
示例:

  class Program
  {
    static void EditEntities()
    {
      using (var editContext = new SchoolEntities())
      {
        var course = editContext.Courses.Where(c => c.CourseID == 1045).FirstOrDefault();
        course.Title = "Calculus New 2";
        var dpt = course.Department;
        dpt.Name = "Mathematics New 2";
        editContext.SaveChanges();
      }
    }

    static void Main(string[] args)
    {
      using (var readContext = new SchoolEntities())
      {
        var course = readContext.Courses.Where(c => c.CourseID == 1045).FirstOrDefault();
        Console.WriteLine(course.Title);
        var dpt = course.Department;
        Console.WriteLine(dpt.Name);

        EditEntities();

        readContext.Courses.MergeOption = MergeOption.OverwriteChanges;
        course = readContext.Courses.Where(c => c.CourseID == 1045).FirstOrDefault();
        Console.WriteLine(course.Title);

        readContext.Departments.MergeOption = MergeOption.OverwriteChanges;
        dpt = readContext.Departments.Where(d => d.DepartmentID == course.DepartmentID).FirstOrDefault();
        Console.WriteLine(dpt.Name);
      }
    }
  }

 


这篇关于EF4 POCO在另一个上下文上完成更新时不更新关系。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆