C# 如何通过实体框架中的反射刷新对象集?

C# 如何通过实体框架中的反射刷新对象集?,c#,entity-framework,reflection,C#,Entity Framework,Reflection,我正在尝试通过反射和EntitySetName进行常规刷新 因此,有效的方法是: base.Refresh(RefreshMode.StoreWins, this.Selections); 我想做的是: base.Refresh(RefreshMode.StoreWins, this.GetType().GetProperty("Selections").GetValue(this, null)); 但这给了我以下例外: 要刷新的对象集合中索引X处的元素具有null EntityKey属性值

我正在尝试通过反射和EntitySetName进行常规刷新

因此,有效的方法是:

base.Refresh(RefreshMode.StoreWins, this.Selections);
我想做的是:

base.Refresh(RefreshMode.StoreWins, this.GetType().GetProperty("Selections").GetValue(this, null));
但这给了我以下例外:

要刷新的对象集合中索引X处的元素具有null EntityKey属性值或未附加到此ObjectStateManager


有什么方法可以让它工作吗?

GetValue只返回一个
对象,而Refresh(显然)不会在内部将其转换为实际类型。如果您想在方法中执行此强制转换,您就回到了起点(必须知道
this.Selections

您唯一的机会是创建一个通用方法,如
Refresh
,这样您就可以直接刷新
ObjectSet

base.Refresh(RefreshMode.StoreWins, this.CreateObjectSet<T>());
base.Refresh(RefreshMode.StoreWins,this.CreateObjectSet());