2

I have a LINQ Query that returns a single object - the context is an ObjectContext not a DbContext

var q = from c in context.x //this has various includes but this just an example
where c.Id == xId
select c;

X x = q.FirstOrDefault();

I am then attaching a collection to this by using the following

ObjectQuery<Z> y = x.Y.CreateSourceQuery().Include("1").Include("2");
x.Y.Attach(y);

However there is a problem where I attempt to refresh the loaded object and if any items attached in the source query were removed they do not change on refresh.I believe this is because the context only loads each object once and keeps it cached, however I need to keep the context open as I need to save back to the database.

I have attempted to refresh using the following:

context.Refresh(RefreshMode.StoreWins, y);

or triggering a triggering a refresh by setting the entity state to modified:

context.ObjectStateManager.ChangeObjectState(pb, EntityState.Modified);

I understand that with a DB context you can refresh the entity forcing a database refresh, my question would be is it possible to force the refresh using ObjectContext.

Using DbContext we were able to get the desired results by refreshing everything however as you can imagine this was very slow.

var refreshableObjects = context.ChangeTracker.Entries().Select(e => e.Entity).ToList();
foreach (var obj in refreshableObjects)
{
    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.GetRelationshipManager(obj).GetAllRelatedEnds().Where(r => r.IsLoaded).ToList().ForEach(c => c.Load());
}
Chris
  • 915
  • 8
  • 28
  • Have you tried this? `context.Entry(y).Reload();` – lokusking Jul 11 '16 at 11:45
  • objectcontext does not have an entry method http://stackoverflow.com/questions/11032683/objectcontext-does-not-contain-a-definition-for-entry-and-no-extension-metho – Chris Jul 11 '16 at 11:50
  • Ye you're right. I look in a couple of hours when i've got access to my EF-Project – lokusking Jul 11 '16 at 11:55
  • 2
    Had you alerady a look at [this](http://stackoverflow.com/a/17092302/4558029)? – lokusking Jul 11 '16 at 18:11
  • I have not, I will give this a go when I can :) – Chris Jul 12 '16 at 11:05
  • Can you take a look at [this](http://stackoverflow.com/questions/18169970/how-do-i-refresh-dbcontext) please? I know it's used a DbContext, but it may help you, as it described improved performance – meJustAndrew Jul 12 '16 at 19:16
  • @meJustAndrew sorry this is of no use as I understand it is simple todo with DbContext but we have no plans to upgrade our Model's .Net version – Chris Jul 13 '16 at 08:34
  • @lokusking this appears to be working, need to put this through testing but hopefully it holds up, if you want to post it as an answer so I can award the bounty if appropriate :) – Chris Jul 13 '16 at 08:35

1 Answers1

0

According to this post, this might help:

public void RefreshAll()
{
     // Get all objects in statemanager with entityKey 
     // (context.Refresh will throw an exception otherwise) 
     var refreshableObjects = (from entry in context.ObjectStateManager.GetObjectStateEntries(
                                                 EntityState.Deleted 
                                               | EntityState.Modified 
                                               | EntityState.Unchanged)
                                      where entry.EntityKey != null
                                      select entry.Entity);

     context.Refresh(RefreshMode.StoreWins, refreshableObjects);
}
Community
  • 1
  • 1
lokusking
  • 7,396
  • 13
  • 38
  • 57
  • this works on some of our cases, one in particular where this doesn't work is setting a relationship to null, this will not refresh it. I will mark as the answer however it is still incomplete. – Chris Jul 13 '16 at 10:35
  • 1
    Maybe dont set relationship to null. Instead clear them or remove an element. Setting an `IEnumerable` to `null` is never a good idea – lokusking Jul 13 '16 at 10:42
  • awarded bounty, we will attempt to fix the additional issues, thanks, chris – Chris Jul 13 '16 at 11:05