Coder Social home page Coder Social logo

Comments (5)

gnaeus avatar gnaeus commented on May 22, 2024

@thoraj, can you give an example of your scenario? I don't understand what do you mean.

Suppose we have an entity Post with CreatorUserId and CreatedUtc properties.
If we call context.Update(post), the following SQL will be generated during SaveChanges:

UPDATE Posts
SET CreatorUserId = @p0,
    CreatedUtc = @p1
    ...
WHERE Id = @p2;

But we get same SQL if we manually owerwrite post.CreatedUtc = new DateTime(2018, 1, 1);

If we load Post from database, the CreatedUtc property already contains a date. Then context.Update marks all properties as changed. And UPDATE statement doesn't really change it's value.

CreatedUtc and CreatorUserId will be erased in DB only if we have detached entity with empty CreatedUtc and CreatorUserId fields. And then we call context.Update() on it. Is this your scenario?

from entityframework.commontools.

thoraj avatar thoraj commented on May 22, 2024

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?

from entityframework.commontools.

thoraj avatar thoraj commented on May 22, 2024

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.

from entityframework.commontools.

gnaeus avatar gnaeus commented on May 22, 2024

You are right. Assigning DateTime.Zero to CreatedUtc and default(TUserId) to CreatorUserId is meaningless. But if we explicitely change CreatedUtc to non-default value, I think we should change it in DB too.

I can add this behaviour in the next release.

from entityframework.commontools.

gnaeus avatar gnaeus commented on May 22, 2024

Fixed in 2.0.2

from entityframework.commontools.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.