Home > other >  EFCore: Is it possible to track changes made to the properties of a newly created and added to the D
EFCore: Is it possible to track changes made to the properties of a newly created and added to the D

Time:11-04

public class Person
{
    public int Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public DateTime DOB { get; set; } 

    public Person()
    {
        Id = 0;
        LastName = String.Empty;
        FirstName = String.Empty;
        DOB = DateTime.Now;
    }
}



private void button1_Click(object sender, EventArgs e)
{
    var v = new Person(){
                    LastName = "LLLLL";
                    FirstName = "FFFFF";
                    DOB = DateTime.Now;
                };

    _dbContext.Add(v);




    // now let's say that the user changes the v.LastName to ...
    v.LastName = "QQQQQQQQQQQQQQQQQQQQQQQQQQ";

    /* now I would expect _dbContext to be already aware of changes 
       made to the v.LastName property or at least to have passed the initial
       values of the v entity to the OriginalValues of the added entry
       and EFCore to providing me with a method to informing me, 
       whenever I asked so,
       if some property values in the entity, being tracked, have been changed!!
    */


   //so whenever I type
   var changesMadeSoFar = HelloLovelyEFCore_DoWeHaveAnyChangesToThePropertiesOfThisLovelyEntryMadeSoFar(EntityEntry v);

  // Unfortunatelly I can't find a way to be inormed of any changes in the properties
  // of the entry being tracked!! Unless I am missing something... (which is very possible)

  //  Even the following fails to provide me information of property changes
      made to a NEWLY created entity NOT a loaded for the DB ones.....

    EntityEntry<TFlatVisit> entry = _dbContext.Entry(v);
    entry.DetectChanges();
    var modified = entry.Members.Where(m => m.IsModified).ToList();
    var modified2 = entry.Properties.Where(m => m.CurrentValue != m.OriginalValue).ToList();
    var modified3 = entry.Properties.Where(m => m.CurrentValue == m.OriginalValue).ToList();

    var modified4 = entry.Properties
        .Where(prop => prop.IsModified)
        .Select(prop => new
        {
            Property = prop.Metadata,
            Value = prop.CurrentValue,
            Name = prop.Metadata.Name,
            PrevValue = prop.OriginalValue
        }).ToList();

 
    if (_dbContext.ChangeTracker.HasChanges())
    {
        _dbContext.ChangeTracker.DetectChanges();
        Debug.WriteLine(_dbContext.ChangeTracker.DebugView.LongView);
    }// the latter results into _dbContext has changed and the state of entry v is set to Added (we all know that)

}

PS: I am not asking to get if the entry has changed!! The entry state is changed to Added. We all know that!! I am specifically asking for a way to know if some or any of the entry's properties have changed. And I would like to get this info via EFCore!! Not via INotifyPropertyChanged and things like that. Hope to have made it clear to you what I am after...

PS2: I am NOT asking for changes made to an entity loaded from the database using _dbContext. I am specifically asking for information on changes made to the properties of a newly created entry.

It appears that the _dbContext and EFCore do not pass the initial values of the newly created entry's properties to the OriginalValues!! and I can not figure out why is it so?!? Wouldn't it be reasonable to have the newly created entry's OriginalValues set to the initial values of the object added to the _dbContext?!? so to giving us the chance of detecting changes to the properties by comparing the original values to the current ones?!?!

Thanx for your time

CodePudding user response:

I can not figure out why is it so?

The ChangeTracker's job to determine what DML statement to run to synchronize the entity with the database. Tracking the changes made to Added is not necessary to this accomplish this goal, and has a non-zero performance cost.

If you want, you can .Attach the entity if you want EF to track the changes, then change the state to Added before SaveChanges. But change tracked entities must have a key set, so if the key is database generated you would have to set it to a non-zero value, and reset it to 0 if you change the state to Added.

  • Related