Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow explicit temporary values that are stored in the entity instance #24245

Closed
ajcvickers opened this issue Feb 23, 2021 · 2 comments · Fixed by #24620
Closed

Allow explicit temporary values that are stored in the entity instance #24245

ajcvickers opened this issue Feb 23, 2021 · 2 comments · Fixed by #24620
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Member

While an application is free to manipulate and set temporary values, those values will never be reflected in the entity instance since the current code requires a CLR default value in the entity instance before it will consider a value temporary. This makes it difficult to work with explicit temporary values outside the context of EF Core where the values need to be set into the entities even after EF Core is tracking them.


Originally reported by @Zero3 here: #12378 (comment)

you are free to set the temporary value into the entity if you want it there

I tried this, but as mentioned above, EF will then save the temporary value to database. And if one manually does context.Entry(entity).Property(e => e.Id).IsTemporary = true;, then EF resets Id back to 0, and we are back where we started.

Here is an example of a custom .Add() method that doesn't work because of this behaviour:

public EntityEntry<EntityBase> AddWithTemporaryId(EntityBase entity)
{
	EntityEntry<EntityBase> entityEntry = base.Add(entity);

	Console.WriteLine("Initial entity value: " + entity.Id);
	Console.WriteLine("Is value temporary: " + entityEntry.Property(e => e.Id).IsTemporary);
	Console.WriteLine("EF temporary value: " + entityEntry.Property(e => e.Id).CurrentValue);
	
	Console.WriteLine("Setting entity value to temporary value");
	entity.Id = entityEntry.Property(e => e.Id).CurrentValue;
	
	Console.WriteLine("Entity value: " + entity.Id);
	Console.WriteLine("Is value temporary: " + entityEntry.Property(e => e.Id).IsTemporary);
	
	Console.WriteLine("Marking entity value as temporary");
	entityEntry.Property(e => e.Id).IsTemporary = true;
	
	Console.WriteLine("Is value temporary: " + entityEntry.Property(e => e.Id).IsTemporary);
	Console.WriteLine("Entity value: " + entity.Id);
	
	return entityEntry;
}

Output:

Initial entity value: 0
Is value temporary: True
EF temporary value: -2147482647
Setting entity value to temporary value
Entity value: -2147482647
Is value temporary: False
Marking entity value as temporary
Is value temporary: True
Entity value: 0
@ajcvickers ajcvickers self-assigned this Feb 23, 2021
@ajcvickers ajcvickers added this to the Backlog milestone Mar 1, 2021
@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Apr 7, 2021
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed consider-for-current-release labels Apr 7, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-preview4 Apr 8, 2021
ajcvickers added a commit that referenced this issue Apr 8, 2021
This allows values explicitly set by the application as temporary to be stored in and obtained from the entity instance.

Fixes #23191
Fixes #24245
ajcvickers added a commit that referenced this issue Apr 19, 2021
This allows values explicitly set by the application as temporary to be stored in and obtained from the entity instance.

Fixes #23191
Fixes #24245
ajcvickers added a commit that referenced this issue Apr 19, 2021
This allows values explicitly set by the application as temporary to be stored in and obtained from the entity instance.

Fixes #23191
Fixes #24245
@Zero3
Copy link

Zero3 commented Jun 8, 2021

@ajcvickers I tried 6.0.0-preview4, and see the same behaviour as posted above. Should I be doing something different?

@ajcvickers
Copy link
Member Author

@Zero3 Please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

@ajcvickers ajcvickers modified the milestones: 6.0.0-preview4, 6.0.0 Nov 8, 2021
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants