Update specific entity entry in the EF Core contextEither get local entity or attach a new oneEntity Framework vs LINQ to SQLHitting an exception while updating an ENTITY, using EntityEntry in .Net CoreEF 6 ChangeTracker Eliminate Duplicate Entity EntriesEFCore throws System.InvalidOperationException when trying to attach non-tracked entryHow to save changes twice in one method using Entity Framework CoreEF Core Update The instance of entity type 'Ads' cannot be trackedEntity Famework core - The instance of entity type cannot be tracked because another instance of this type with the same key is already being trackedEF Core - Insert then update an entryUpdate range entity after adding new entity in EF Core 2Unable to update child entities using Entity Framework Core 2.1
Should I cheat if the majority does it?
Book series about using wormholes and time travel to reduce decades of space travel to days
Why did C++11 make std::string::data() add a null terminating character?
Do intermediate subdomains need to exist?
Why does the Batman "crack his knuckles" in "Batman: Arkham Origins"?
How might boat designs change in order to allow them to be pulled by dragons?
Did Snape really give Umbridge a fake Veritaserum potion that Harry later pretended to drink?
Does taking on an assistant professor position prevent me from doing post-docs later?
Why would a propellor have blades of different lengths?
Is it bad to suddenly introduce another element to your fantasy world a good ways into the story?
Is it possible that Curiosity measured its own methane or failed doing the spectrometry?
How did Einstein know the speed of light was constant?
Explain how 'Sharing the burden' puzzle from Professor Layton and the Miracle Mask should be solved
Why do we need a bootloader separate than our application program in MCU's?
Should I warn my boss I might take sick leave
What is meant by perfect, imperfect consonance and dissonance?
What is this arch-and-tower near a road?
how to convert Timestring to seconds
Is it possible to spoof an IP address to an exact number?
Can 4 Joy cons connect to the same Switch?
What/Where usage English vs Japanese
Chess problem: Make a crossword in 3 moves
How can power levels matter in a magic system that emphasizes control?
Performance of loop vs expansion
Update specific entity entry in the EF Core context
Either get local entity or attach a new oneEntity Framework vs LINQ to SQLHitting an exception while updating an ENTITY, using EntityEntry in .Net CoreEF 6 ChangeTracker Eliminate Duplicate Entity EntriesEFCore throws System.InvalidOperationException when trying to attach non-tracked entryHow to save changes twice in one method using Entity Framework CoreEF Core Update The instance of entity type 'Ads' cannot be trackedEntity Famework core - The instance of entity type cannot be tracked because another instance of this type with the same key is already being trackedEF Core - Insert then update an entryUpdate range entity after adding new entity in EF Core 2Unable to update child entities using Entity Framework Core 2.1
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm using DbContext scope implementation. Basically it means that I relay on ambient DbContext approach. More information
I simplified my real code and removed all logic related to the context flow behavior...
using (var dbContextParentScope = ...)
DbContext parentContext = dbContextParentScope.Get<ApplicationContext>();
// that data will be in the DbContext cache
var accounts = parentContext.Accounts.ToList();
// this is always part of a separate method
using (var dbContextChildScope = ...)
DbContext childContext = dbContextChildScope.Get<ApplicationContext>();
var childAccounts = childContext.Accounts.ToList();
childAccounts.ForEach(a => a.DisplayName = "New name");
// after that operation parentContext data will be outdated
childContext.SaveChanges();
// this will reload updated entities in the parentContext
dbContextChildScope.RefreshEntitiesInParentScope(childAccounts);
// without Refresh operation we will work with outdated data
parentContext.SaveChanges();
This is exceptional case but sometimes we have to save important information outside main transaction.
Entity Framework has possibility to get record by key from DbContext:
if (((IObjectContextAdapter)childContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(childAccounts[0], out ObjectStateEntry stateInChildScope))
var key = stateInChildScope.EntityKey;
if (((IObjectContextAdapter)parentContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(key, out ObjectStateEntry stateInParentScope))
if (stateInParentScope.State == EntityState.Unchanged)
((IObjectContextAdapter)parentContext).ObjectContext.Refresh(RefreshMode.StoreWins, stateInParentScope.Entity);
EF Core doesn't have such functionality but it has 'ChangeTracker'. It can return entries that DbContext is tracking:
parentContext.ChangeTracker.Entries().Where(w => w.State == EntityState.Unchanged).ToList()
But I want to reload only records that were updated in the childContext. So if I write something like this:
var updatedEntry = childContext.Entry(childAccounts[0]);
parentContext.Entry(updatedEntry.Entity).Reload();
EF Core thinks about updatedEntry like it is new and will throw System.InvalidOperationException:
The instance of entity type 'Account' cannot be tracked because another instance with the same key value for 'EntityId' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
Also I found some internal functionality but I don't want to use internal EF Core code (except it is only way to make it works) because it can be changed/removed:
var entityType = parentContext.Model.FindEntityType(typeof(TEntityType));
var key = entityType.FindPrimaryKey();
var stateManager = parentContext.GetDependencies().StateManager;
var keysDictionary = key.Properties.ToDictionary(x => x.Name, x => x.PropertyInfo.GetValue(childAccounts[0]));
var entry = stateManager.TryGetEntry(key, keysDictionary.Values.ToArray());
parentContext.Entry(entry.Entity).Reload();
Does anyone know how can I update entities from parent context?
I hope for your help because I'm stuck here and I ran out of any ideas
entity-framework entity-framework-core dbcontext
add a comment |
I'm using DbContext scope implementation. Basically it means that I relay on ambient DbContext approach. More information
I simplified my real code and removed all logic related to the context flow behavior...
using (var dbContextParentScope = ...)
DbContext parentContext = dbContextParentScope.Get<ApplicationContext>();
// that data will be in the DbContext cache
var accounts = parentContext.Accounts.ToList();
// this is always part of a separate method
using (var dbContextChildScope = ...)
DbContext childContext = dbContextChildScope.Get<ApplicationContext>();
var childAccounts = childContext.Accounts.ToList();
childAccounts.ForEach(a => a.DisplayName = "New name");
// after that operation parentContext data will be outdated
childContext.SaveChanges();
// this will reload updated entities in the parentContext
dbContextChildScope.RefreshEntitiesInParentScope(childAccounts);
// without Refresh operation we will work with outdated data
parentContext.SaveChanges();
This is exceptional case but sometimes we have to save important information outside main transaction.
Entity Framework has possibility to get record by key from DbContext:
if (((IObjectContextAdapter)childContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(childAccounts[0], out ObjectStateEntry stateInChildScope))
var key = stateInChildScope.EntityKey;
if (((IObjectContextAdapter)parentContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(key, out ObjectStateEntry stateInParentScope))
if (stateInParentScope.State == EntityState.Unchanged)
((IObjectContextAdapter)parentContext).ObjectContext.Refresh(RefreshMode.StoreWins, stateInParentScope.Entity);
EF Core doesn't have such functionality but it has 'ChangeTracker'. It can return entries that DbContext is tracking:
parentContext.ChangeTracker.Entries().Where(w => w.State == EntityState.Unchanged).ToList()
But I want to reload only records that were updated in the childContext. So if I write something like this:
var updatedEntry = childContext.Entry(childAccounts[0]);
parentContext.Entry(updatedEntry.Entity).Reload();
EF Core thinks about updatedEntry like it is new and will throw System.InvalidOperationException:
The instance of entity type 'Account' cannot be tracked because another instance with the same key value for 'EntityId' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
Also I found some internal functionality but I don't want to use internal EF Core code (except it is only way to make it works) because it can be changed/removed:
var entityType = parentContext.Model.FindEntityType(typeof(TEntityType));
var key = entityType.FindPrimaryKey();
var stateManager = parentContext.GetDependencies().StateManager;
var keysDictionary = key.Properties.ToDictionary(x => x.Name, x => x.PropertyInfo.GetValue(childAccounts[0]));
var entry = stateManager.TryGetEntry(key, keysDictionary.Values.ToArray());
parentContext.Entry(entry.Entity).Reload();
Does anyone know how can I update entities from parent context?
I hope for your help because I'm stuck here and I ran out of any ideas
entity-framework entity-framework-core dbcontext
No public way so far. The internal functionality you found is similar to what I used here stackoverflow.com/questions/50748522/…, so encapsulate it in a custom method and update the implementation accordingly if/when EF Core code changed.
– Ivan Stoev
Mar 25 at 19:10
Thanks @IvanStoev. You helped me again. I've noticed strange behavior related to the entity status which I can't explain. I've created stateManager from parentContext. TryGetEntry returns correct entry.Entity but entry.EntityState has incorrect value (Unchanged instead Modified). But parentContext.Entry(entry.Entity) returns correct State (Modified because I've changed entity in the parentContext). Have you seen information about it somewhere?
– Pavel
Mar 28 at 17:12
1
ProbablyparentContext.Entrymethod calls internallyDetectChangesfirst (see this v3.0 change docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/…). If you need the correct State (in my code I needed just the entity), probably you should callparentContext.ChangeTracker.DetectChanges();somewhere.
– Ivan Stoev
Mar 28 at 17:35
Thanks.parentContext.ChangeTracker.DetectChanges();works and helped to optimize my code.
– Pavel
Mar 28 at 18:08
add a comment |
I'm using DbContext scope implementation. Basically it means that I relay on ambient DbContext approach. More information
I simplified my real code and removed all logic related to the context flow behavior...
using (var dbContextParentScope = ...)
DbContext parentContext = dbContextParentScope.Get<ApplicationContext>();
// that data will be in the DbContext cache
var accounts = parentContext.Accounts.ToList();
// this is always part of a separate method
using (var dbContextChildScope = ...)
DbContext childContext = dbContextChildScope.Get<ApplicationContext>();
var childAccounts = childContext.Accounts.ToList();
childAccounts.ForEach(a => a.DisplayName = "New name");
// after that operation parentContext data will be outdated
childContext.SaveChanges();
// this will reload updated entities in the parentContext
dbContextChildScope.RefreshEntitiesInParentScope(childAccounts);
// without Refresh operation we will work with outdated data
parentContext.SaveChanges();
This is exceptional case but sometimes we have to save important information outside main transaction.
Entity Framework has possibility to get record by key from DbContext:
if (((IObjectContextAdapter)childContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(childAccounts[0], out ObjectStateEntry stateInChildScope))
var key = stateInChildScope.EntityKey;
if (((IObjectContextAdapter)parentContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(key, out ObjectStateEntry stateInParentScope))
if (stateInParentScope.State == EntityState.Unchanged)
((IObjectContextAdapter)parentContext).ObjectContext.Refresh(RefreshMode.StoreWins, stateInParentScope.Entity);
EF Core doesn't have such functionality but it has 'ChangeTracker'. It can return entries that DbContext is tracking:
parentContext.ChangeTracker.Entries().Where(w => w.State == EntityState.Unchanged).ToList()
But I want to reload only records that were updated in the childContext. So if I write something like this:
var updatedEntry = childContext.Entry(childAccounts[0]);
parentContext.Entry(updatedEntry.Entity).Reload();
EF Core thinks about updatedEntry like it is new and will throw System.InvalidOperationException:
The instance of entity type 'Account' cannot be tracked because another instance with the same key value for 'EntityId' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
Also I found some internal functionality but I don't want to use internal EF Core code (except it is only way to make it works) because it can be changed/removed:
var entityType = parentContext.Model.FindEntityType(typeof(TEntityType));
var key = entityType.FindPrimaryKey();
var stateManager = parentContext.GetDependencies().StateManager;
var keysDictionary = key.Properties.ToDictionary(x => x.Name, x => x.PropertyInfo.GetValue(childAccounts[0]));
var entry = stateManager.TryGetEntry(key, keysDictionary.Values.ToArray());
parentContext.Entry(entry.Entity).Reload();
Does anyone know how can I update entities from parent context?
I hope for your help because I'm stuck here and I ran out of any ideas
entity-framework entity-framework-core dbcontext
I'm using DbContext scope implementation. Basically it means that I relay on ambient DbContext approach. More information
I simplified my real code and removed all logic related to the context flow behavior...
using (var dbContextParentScope = ...)
DbContext parentContext = dbContextParentScope.Get<ApplicationContext>();
// that data will be in the DbContext cache
var accounts = parentContext.Accounts.ToList();
// this is always part of a separate method
using (var dbContextChildScope = ...)
DbContext childContext = dbContextChildScope.Get<ApplicationContext>();
var childAccounts = childContext.Accounts.ToList();
childAccounts.ForEach(a => a.DisplayName = "New name");
// after that operation parentContext data will be outdated
childContext.SaveChanges();
// this will reload updated entities in the parentContext
dbContextChildScope.RefreshEntitiesInParentScope(childAccounts);
// without Refresh operation we will work with outdated data
parentContext.SaveChanges();
This is exceptional case but sometimes we have to save important information outside main transaction.
Entity Framework has possibility to get record by key from DbContext:
if (((IObjectContextAdapter)childContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(childAccounts[0], out ObjectStateEntry stateInChildScope))
var key = stateInChildScope.EntityKey;
if (((IObjectContextAdapter)parentContext).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(key, out ObjectStateEntry stateInParentScope))
if (stateInParentScope.State == EntityState.Unchanged)
((IObjectContextAdapter)parentContext).ObjectContext.Refresh(RefreshMode.StoreWins, stateInParentScope.Entity);
EF Core doesn't have such functionality but it has 'ChangeTracker'. It can return entries that DbContext is tracking:
parentContext.ChangeTracker.Entries().Where(w => w.State == EntityState.Unchanged).ToList()
But I want to reload only records that were updated in the childContext. So if I write something like this:
var updatedEntry = childContext.Entry(childAccounts[0]);
parentContext.Entry(updatedEntry.Entity).Reload();
EF Core thinks about updatedEntry like it is new and will throw System.InvalidOperationException:
The instance of entity type 'Account' cannot be tracked because another instance with the same key value for 'EntityId' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
Also I found some internal functionality but I don't want to use internal EF Core code (except it is only way to make it works) because it can be changed/removed:
var entityType = parentContext.Model.FindEntityType(typeof(TEntityType));
var key = entityType.FindPrimaryKey();
var stateManager = parentContext.GetDependencies().StateManager;
var keysDictionary = key.Properties.ToDictionary(x => x.Name, x => x.PropertyInfo.GetValue(childAccounts[0]));
var entry = stateManager.TryGetEntry(key, keysDictionary.Values.ToArray());
parentContext.Entry(entry.Entity).Reload();
Does anyone know how can I update entities from parent context?
I hope for your help because I'm stuck here and I ran out of any ideas
entity-framework entity-framework-core dbcontext
entity-framework entity-framework-core dbcontext
edited Mar 25 at 19:22
Pavel
asked Mar 25 at 18:08
PavelPavel
1891 gold badge5 silver badges16 bronze badges
1891 gold badge5 silver badges16 bronze badges
No public way so far. The internal functionality you found is similar to what I used here stackoverflow.com/questions/50748522/…, so encapsulate it in a custom method and update the implementation accordingly if/when EF Core code changed.
– Ivan Stoev
Mar 25 at 19:10
Thanks @IvanStoev. You helped me again. I've noticed strange behavior related to the entity status which I can't explain. I've created stateManager from parentContext. TryGetEntry returns correct entry.Entity but entry.EntityState has incorrect value (Unchanged instead Modified). But parentContext.Entry(entry.Entity) returns correct State (Modified because I've changed entity in the parentContext). Have you seen information about it somewhere?
– Pavel
Mar 28 at 17:12
1
ProbablyparentContext.Entrymethod calls internallyDetectChangesfirst (see this v3.0 change docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/…). If you need the correct State (in my code I needed just the entity), probably you should callparentContext.ChangeTracker.DetectChanges();somewhere.
– Ivan Stoev
Mar 28 at 17:35
Thanks.parentContext.ChangeTracker.DetectChanges();works and helped to optimize my code.
– Pavel
Mar 28 at 18:08
add a comment |
No public way so far. The internal functionality you found is similar to what I used here stackoverflow.com/questions/50748522/…, so encapsulate it in a custom method and update the implementation accordingly if/when EF Core code changed.
– Ivan Stoev
Mar 25 at 19:10
Thanks @IvanStoev. You helped me again. I've noticed strange behavior related to the entity status which I can't explain. I've created stateManager from parentContext. TryGetEntry returns correct entry.Entity but entry.EntityState has incorrect value (Unchanged instead Modified). But parentContext.Entry(entry.Entity) returns correct State (Modified because I've changed entity in the parentContext). Have you seen information about it somewhere?
– Pavel
Mar 28 at 17:12
1
ProbablyparentContext.Entrymethod calls internallyDetectChangesfirst (see this v3.0 change docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/…). If you need the correct State (in my code I needed just the entity), probably you should callparentContext.ChangeTracker.DetectChanges();somewhere.
– Ivan Stoev
Mar 28 at 17:35
Thanks.parentContext.ChangeTracker.DetectChanges();works and helped to optimize my code.
– Pavel
Mar 28 at 18:08
No public way so far. The internal functionality you found is similar to what I used here stackoverflow.com/questions/50748522/…, so encapsulate it in a custom method and update the implementation accordingly if/when EF Core code changed.
– Ivan Stoev
Mar 25 at 19:10
No public way so far. The internal functionality you found is similar to what I used here stackoverflow.com/questions/50748522/…, so encapsulate it in a custom method and update the implementation accordingly if/when EF Core code changed.
– Ivan Stoev
Mar 25 at 19:10
Thanks @IvanStoev. You helped me again. I've noticed strange behavior related to the entity status which I can't explain. I've created stateManager from parentContext. TryGetEntry returns correct entry.Entity but entry.EntityState has incorrect value (Unchanged instead Modified). But parentContext.Entry(entry.Entity) returns correct State (Modified because I've changed entity in the parentContext). Have you seen information about it somewhere?
– Pavel
Mar 28 at 17:12
Thanks @IvanStoev. You helped me again. I've noticed strange behavior related to the entity status which I can't explain. I've created stateManager from parentContext. TryGetEntry returns correct entry.Entity but entry.EntityState has incorrect value (Unchanged instead Modified). But parentContext.Entry(entry.Entity) returns correct State (Modified because I've changed entity in the parentContext). Have you seen information about it somewhere?
– Pavel
Mar 28 at 17:12
1
1
Probably
parentContext.Entry method calls internally DetectChanges first (see this v3.0 change docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/…). If you need the correct State (in my code I needed just the entity), probably you should call parentContext.ChangeTracker.DetectChanges(); somewhere.– Ivan Stoev
Mar 28 at 17:35
Probably
parentContext.Entry method calls internally DetectChanges first (see this v3.0 change docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/…). If you need the correct State (in my code I needed just the entity), probably you should call parentContext.ChangeTracker.DetectChanges(); somewhere.– Ivan Stoev
Mar 28 at 17:35
Thanks.
parentContext.ChangeTracker.DetectChanges(); works and helped to optimize my code.– Pavel
Mar 28 at 18:08
Thanks.
parentContext.ChangeTracker.DetectChanges(); works and helped to optimize my code.– Pavel
Mar 28 at 18:08
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344058%2fupdate-specific-entity-entry-in-the-ef-core-context%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344058%2fupdate-specific-entity-entry-in-the-ef-core-context%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
No public way so far. The internal functionality you found is similar to what I used here stackoverflow.com/questions/50748522/…, so encapsulate it in a custom method and update the implementation accordingly if/when EF Core code changed.
– Ivan Stoev
Mar 25 at 19:10
Thanks @IvanStoev. You helped me again. I've noticed strange behavior related to the entity status which I can't explain. I've created stateManager from parentContext. TryGetEntry returns correct entry.Entity but entry.EntityState has incorrect value (Unchanged instead Modified). But parentContext.Entry(entry.Entity) returns correct State (Modified because I've changed entity in the parentContext). Have you seen information about it somewhere?
– Pavel
Mar 28 at 17:12
1
Probably
parentContext.Entrymethod calls internallyDetectChangesfirst (see this v3.0 change docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/…). If you need the correct State (in my code I needed just the entity), probably you should callparentContext.ChangeTracker.DetectChanges();somewhere.– Ivan Stoev
Mar 28 at 17:35
Thanks.
parentContext.ChangeTracker.DetectChanges();works and helped to optimize my code.– Pavel
Mar 28 at 18:08