efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Non-dirty Entity after it loaded

Open yarmenteros opened this issue 10 years ago • 4 comments

I need mark my loaded entities as not dirty. Exists any way to get the entity after hydration is completed where I can set the IsDirty property to false.

IsDirty is an object state property that I use for other rules in my source code. My entities implements the INotifyPropertyChanged interface and when any property is changed the IsDirty property will be set to True.

My entity code sample:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private bool _isDirty;
    private string _name;
    private Address _personalAddress;

    public Guid Id { get; set; }

    public string Name 
    { 
        get { return _name; }
        set 
        {
                if(!_name.Equals(value))
                {
                    _name = value;
                    RaisePropertyChanged("Name");
                }
        }
    }

    public Guid PersonalAddressId { get; set; }

    public virtual Address PersonalAddress
    { 
        get { return _personalAddress; }
        set 
        {
                if(!_personalAddress.Equals(value))
                {
                    _personalAddress= value;
                    RaisePropertyChanged("PersonalAddress");
                }
        }
    }

    public bool IsDirty 
    { 
        get { return _isDirty; } 
    }

    public void MarkAsClean()
    { 
        _isDirty = false; 
    }

    public void RaisePropertyChanged(string propertyName)
    {
           _isDirty = true;
           PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Thanks for your time

yarmenteros avatar Aug 02 '16 20:08 yarmenteros

@yarmenteros I don't think that there is a way to do this currently. We have a feature on our backlog to implement an ObjectMaterialized event that would fire after all the scalar properties read from the database have been set. The issue tacking this is #476. I'm going to close this as a dupe of that issue.

Even with that event, navigation properties can still be set by EF at a later time when EF is fixing up references between entities. So if setting navigations causes your entity to be marked as dirty, then ObjectMaterialized probably won't help. Your best bet in this case is to have some code that you execute after all the querying is complete to go through any mark everything as clean.

It's also worth noting that if EF can find backing fields for all your properties, then it will write directly to those backing fields when materializing an entity from the database. So in this case the _isDirty flag should not get touched.

ajcvickers avatar Aug 03 '16 17:08 ajcvickers

@ajcvickers Thanks for your answer. Taking your words:

_It's also worth noting that if EF can find backing fields for all your properties, then it will write directly to those backing fields when materializing an entity from the database. So in this case the isDirty flag should not get touched.

I did a test sample where I have a navigation property, for example Product inside OrderDetail, the Product property have a custom logic that I don't want to be executed when materialize OrderDetail entity. Also I have the backing field for Product property named _product.

When I debug my test I see that EF is using the Product property to set the product instance instead of use convention to discover _product as backing field to Product property. Even if I use the Fluent API to explicitly configure the backing field for Product property then I getting an error as follow:

System.InvalidOperationException: The property 'Product' cannot be added to the entity type 'VendorProductPrice' because a navigation property with the same name already exists on entity type 'VendorProductPrice'.

So my questions are:

  • Only can I use backing field conventions for scalar properties?
  • It's a bug the limitation of use of backing field for navigation property or it's the desired behavior?

Regards, I apologize for my English

yarmenteros avatar Aug 04 '16 14:08 yarmenteros

@yarmenteros EF does not currently use fields for navigation properties, nor does it use fields for anything done after the initial hydration of scalar fields by a query. We are working on an enhancement that would allow EF to use backing fields everywhere, including for navigation properties.

ajcvickers avatar Aug 04 '16 16:08 ajcvickers

Ok, thanks again

yarmenteros avatar Aug 04 '16 19:08 yarmenteros