-
Notifications
You must be signed in to change notification settings - Fork 17
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
Tracking does not work when using context.Update() #4
Comments
@thoraj, can you give an example of your scenario? I don't understand what do you mean. Suppose we have an entity UPDATE Posts
SET CreatorUserId = @p0,
CreatedUtc = @p1
...
WHERE Id = @p2; But we get same SQL if we manually owerwrite If we load
|
Yes, that is my scenario. I have a modified (detached) entity, and I wish to either create a new if it does not exist, or update if it exists. I'm trying to use EF Core tracking instead of doing manual diffs myself. The code in question looks like this: // check if entity exist
var existingObservation = await _observationCtx.Observations.SingleOrDefaultAsync(o => o.Id == observation.Observation.ObservationId);
if (existingObservation is null)
{
await _observationCtx.AddAsync(obs);
}
else
{
// Remove the entity from tracking
_observationCtx.Entry(existingObservation).State = EntityState.Detached;
// Update the entity (note that by default, this will update all columns).
_observationCtx.Observations.Update(obs);
}
await _observationCtx.SaveChangesAsync(); Before I Update() using the detached entity (obs) I first have to stop tracking the existing (and attached entity). Of course the alternative to Update() is to do a manual merge, but I would rather not do that and have EFCore tracking machinery do this instead. Obviously I am open for suggestions, since the current approach is not working very well. Perhaps the simplest way/compromise is to manually copy the tracking/audit fields from the existing entity? |
I changed the Update() to this: var et =_observationCtx.Observations.Update(obs);
et.Property(p => p.CreatedUtc).IsModified = false;
et.Property(p => p.CreatorUserId).IsModified = false; This will tell EF to not update the properties tracking Creation. |
You are right. Assigning I can add this behaviour in the next release. |
Fixed in 2.0.2 |
EF Core contexts support an Update method which will update an entity if it exist.
When using context.Update(), the columns for tracking CreatorUserId and CreatedUTC are reset.
Is this a known problem?
If so, is this something that is on the roadmap to fix?
The text was updated successfully, but these errors were encountered: