开发者

EF4: How to Reload / Refresh an EntityCollection?

Using Entity Framework 4.0 in C# / .NET 4.0.

Within my Entity model, I have an object (MyObject) that is part of a one-to-many relationship that produces a Navigation Property of the type EntityCollection<OtherObject>. Snippet of the generate code:

public partial class MyObject : EntityObject
{
    /* other code */

    public EntityCollection<OtherObject> OtherObjects
    {
        get { /* RelationshipManager stuff */ }
        set { /* RelationshipManager stuff */ }
    }

    /* other code */
}

I load the data fine, everything is good. Then another process adds lines to the underlying OtherObject table. I want to be able to reload or refresh my entity collection in order to gain access to these new objects.

Is there any possible way to do this? Neither of the following attempts accomplish the task:

Context.Refresh(RefreshMode.StoreWins, myObject);
Context.Refresh(RefreshMode.StoreWins, myObject.OtherObjects);

I'd like to avoid having to unload the entire context (as this would force a save of any currently modified information, which is undesirable), so is there any way to get the newly added data into my loc开发者_运维知识库al entity model?

Thanks.


myObject.OtherObjects.Clear();
Context.AcceptAllChanges();
myObject.OtherObjects.Load();

Disclaimer: Not Tested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜