Question

I've been working on refactoring some EF6 code to EF Core 1, however I've run across a tiny roadblock. Here is the code I'm trying to translate:

https://github.com/mehdime/DbContextScope

The majority of things are good, but DbContextScope.cs in particular is proving challenging. For instance, consider the following procedure (modified for brevity):

        public void RefreshEntitiesInParentScope(IEnumerable entities)
    {
        foreach (IObjectContextAdapter contextInCurrentScope in 
            _dbContexts.InitializedDbContexts.Values)
        {
            var correspondingParentContext =
                _parentScope._dbContexts.InitializedDbContexts.Values
                    .SingleOrDefault(parentContext => 
                        parentContext.GetType() == contextInCurrentScope.GetType())
                           as IObjectContextAdapter;

            if (correspondingParentContext == null)
                continue;

            foreach (var toRefresh in entities)
            {
                ObjectStateEntry stateInCurrentScope;
                if (contextInCurrentScope.ObjectContext.ObjectStateManager
                        .TryGetObjectStateEntry(toRefresh, out stateInCurrentScope))
                {
                    var key = stateInCurrentScope.EntityKey;

                    ObjectStateEntry stateInParentScope;
                    if (correspondingParentContext.ObjectContext.ObjectStateManager
                       .TryGetObjectStateEntry(key, out stateInParentScope))
                    {
                        if (stateInParentScope.State == EntityState.Unchanged)
                        {
                            correspondingParentContext.ObjectContext.Refresh(
                                RefreshMode.StoreWins, stateInParentScope.Entity);
                        }
                    }
                }
            }
        }
    }

Questions.

First, I am aware that ObjectContext is replaceable. While using the new ChangeTracker with ObjectStateManager, I want to make sure the entry I receive is accurate. What would the next line look like in EF Core?

contextInCurrentScope.ObjectContext.ObjectStateManager
    .TryGetObjectStateEntry(toRefresh, out stateInCurrentScope)

Second, how would this translate into EF Core?

correspondingParentContext.ObjectContext.Refresh

Thanks!

P.S. The source code in the GitHub repository linked above has a lot of useful comments.

1
2
7/18/2016 10:09:42 PM

Popular Answer

I believe the proper method for obtaining an entity's entry and, subsequently, its keys and state is through:

var entry = contextInCurrentScope.Entry(toRefresh);
var keys = entry.Metadata.GetKeys();
var state = entry.State;

Using the following entry, you may also refresh a single entity from the database:

entry.Reload();
1
12/12/2018 9:05:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow