-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Generate All Possible Values Other Than PKs When Entities Are Tracked From Detached State #22149
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Generate All Possible Values Other Than PKs When Entities Are Tracked From Detached State #22149
Conversation
…ed value if it is incorrectly empty when entity tracking state changes occur. This is done via the creation and provisioning of a custom state manager (IStateManager) service containing the logic.
@ajcvickers Might provide some thoughts |
Do you think this is something we can start doing always? |
Yes, it might be a minor breaking change, but I can't come up with a scenario where this would be blocking |
Is this behavior of populating the |
Will start working on this anyways. |
While it's most evident in Cosmos it would also be used for data seeding across all providers. |
The logic needs to also happen on update for the Cosmos DB provider, because otherwise it will also cause entity tracking to throw. Also, how to I restrict foreach (var property in entry.EntityType.GetProperties().Where(property => !property.IsPrimaryKey() && entry.GetCurrentValue(property) is null))
{
var valueGenerator = property.GetValueGeneratorFactory()(property, entry.EntityType);
entry.SetStoreGeneratedValue(property, valueGenerator.Next(entry.ToEntityEntry()));
} Can I just change |
Yes
Only if changing from Detached |
…nerateAsync methods, add logic to StateManager to use ValueGenerationManager to generate values if necessary for non-PK properties on entities which change from the Detached state to the Modified or Unchanged state, extend test to include checking that the Update method also works on the context, create and use test-specific context IdentifierShadowValuePresenceTestContext in test, remove definition and use of CosmosStateManager, and rename test database to IdentifierShadowValuePresenceTest.
I added |
…tity deletion via normal use of DbContext Remove method works, make other minor changes to test, and rename test to have more accurate declared success condition.
Had to make value generation occur even when entities are newly tracked in the |
Do I need to check that |
You should do it here instead |
What do you mean? I was just asking whether or not it matters that generation does not occur in the if (adding || oldState is EntityState.Detached)
{
StateManager.ValueGenerationManager.Generate(this, includePKs: adding);
} |
This seems to work. |
…ure information semantics than available with Assert True where reasonable, remove value creation from StateManager StateChanging and into core InternalEntityEntry logic within SetEntityState and SetEntityStateAsync methods.
I have added a baseline test to make sure that the |
@TheFanatr You understood me correctly |
Is the |
src/EFCore.Cosmos/Extensions/CosmosServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
No, we've been generating values when changing to Added from states other than Detached, better not to change this behavior |
Thanks for your contribution! |
Makes it so a value for
__id
is created on entities which were in aDetached
state but are then tracked in a new state on anEFCore.Cosmos
-backed context;DbContext
methods used to transition detached entities into another state would not otherwise work, other thanAdd
andAddAsync
. This is discussed in #15289.The initial fix for this was local to
EFCore.Cosmos
, but was then it was modified and written intoEFCore
, so that now all non-PK values will be created when detached entities are tracked in another state.Fixes #15289