实体框架4.4,重新加载Child集合对象

ado*_*ero 5 caching parent parent-child reload entity-framework-4

我一直在寻找一段时间,没有什么我能找到的.我有一种情况,我有两个对象(见下面的定义).子对象是父对象的集合.我正在使用WPF/Prism和Entity Framework 4.4在Visual Studio 2012中工作

class Parent
{
    ...other properties...
    public virtual ICollection<Child> Children { get; set; }
} 

class Child
{
    public string Value1 { get; set }
    public string Value2 { get; set }
    public string Value3 { get; set }
}
Run Code Online (Sandbox Code Playgroud)

我有一个表单,其中父项加载到左侧的列表框中,用户单击列表中的一个项目,我们在右侧显示属性.一切都很好用直到我们有两台机器打到同一个数据库.如果一台机器添加或更新其中一条记录,我遇到第二台机器获取数据的问题......

我有以下代码来尝试修复它,但似乎在实体框架中应该有一个简单的方法.

DbEntityEntry<Parent> entry = dbContext.Entry(parent);
entry.Reload(); //Note: this refreshes the properties on the Parent object (but not the collection

if (Parent.Children != null)
{
   Array a = doc.Children.ToArray<Child>();  //this is here because they may have deleted one of the records 

   foreach (Child g in a)
   {
       DbEntityEntry<Child> c= dbContext.Entry(g);
       c.Reload();  //Note: if it is deleted, the Parent.Child gets updated automatically to remove the record.
   }
}

entry.Collection(o => o.Children ).Load(); //call this to get new records
Run Code Online (Sandbox Code Playgroud)

我有什么想法我能做得更好还是这样做的方法?????

ado*_*ero 12

将此移至答案

找到了一种更好的方法来在阵列上面做"foreach"

((IObjectContextAdapter)dbContext).ObjectContext.Refresh(RefreshMode.StoreWins, parent.Children); 
Run Code Online (Sandbox Code Playgroud)

这将删除条目并更新当前条目.仍需要.Load()来获取新记录.这似乎也快得多