Entity Framework 4.1 - Refresh is not a member of Context

元气小坏坏 提交于 2019-11-30 03:20:32

问题


I'm trying to revert Context changes using the Context.Refresh method but It seems like Refresh is not a member of Context.

I'm using the Microsoft ADO.NET Entity Framework 4.1 RC version.

Any idea?


回答1:


You are likely looking at a DbContext, which does not have a Refresh method. You can use the IObjectContextAdapter interface to get the underlying ObjectContext, and call Refresh on that.

var objectContext = ((IObjectContextAdapter)context).ObjectContext;



回答2:


You can also use the Reload function on the Proxy Objects... Here is a sample to reload all modified objects:

            var modifiedEntries = context.ChangeTracker.Entries()
                .Where(e => e.State == EntityState.Modified);
            foreach (var modifiedEntry in modifiedEntries) {
                modifiedEntry.Reload();
            }



回答3:


The answer posted in this thread might help too: Refresh entity instance with DbContext

In summary though, you might try calling something like the following:

dbContext.Entry(someEntityObjectInstance).Reload();

However, someone else noted that this doesn't refresh navigation properties, so if you have to worry about refreshing navigation properties as well, you either need to Reload() all of the navigation properties as well or you will need to Detach() or Refresh() after casting to IObjectContextAdapter, or maybe just recreate your DbContext.

In my case, I frankly decided it was best just to recreate the context and re-Find() the entity:

dbContext = new Model.Entities();
someEntityObjectInstance = dbContext.SomeEntityType.Find(someEntityObjectInstanceKey);

There is arguably no simple/best answer here.



来源:https://stackoverflow.com/questions/7863270/entity-framework-4-1-refresh-is-not-a-member-of-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!