Non-persistent property refresh

Thread is closed for posting
2 posts, 0 answers
  1. Paresh
    Paresh avatar
    13 posts
    Member since:
    Jul 2014

    Posted 06 Oct 2015 Link to this post


    dbcontext.Refresh doesn't refresh/reset non-persistent properties of persistent class, how this can be done?


    Class User


       public string PersistentProperty1 {get;set;}

       public string PersistentProperty2 {get;set;}

       public string Non-PersistentProperty {get;set;}​​



    var userObject = new User();

    userObject.PersistentProperty1 = "Test";

    userObject.PersistentProperty2 = "Test2";

    userObject.Non-PersistentProperty = "Test3";

    now if I call dbcontext.Refresh(userObject, OverwriteChangesFromStore);

    only persistent properties are getting reset, non-persistent property is still set to "Test3".


    How can I load complete userObject from DB, so that non-persistent property is set to string.empty or null?



  2. Simeon Simeonov
    Simeon Simeonov avatar
    24 posts

    Posted 09 Oct 2015 Link to this post

    Hi Paresh,

    Thank you for contacting us. The behavior you are experiencing is expected. No data is stored for your non-persisted properties in the database and for this reason DataAccess cannot reset them to their initial state.

    Exactly for such cases we have the IInstanceCallbacks interface. It allows you to plug your custom logic just after an object is fetched/refreshed from the database. Please consider the following example:
    public partial class Car : IInstanceCallbacks
        public void PostLoad()
            // Plug here logic you want to execute after the object is resolved and loaded in memory (fetched/refreshed)
            this.CarYear = null;
        public void PreRemove(IObjectScope objectScope)
            // Plug here logic that you want to execute before removing the object from the database
        public void PreStore()
            // Plug here logic that you want to execute before storing the object from the database

    In this example I have a Car entity and for it the CarYear is a non-persisted property. I have extended the Car class and Implemented the IInstanceCallbacks interface. What I need from this interface is the PostLoad method. It is called after the object is fetched/refreshed and allows me to initialize the transient properties. In the example above I am initializing the CarYear property to null.

    I hope you find this information useful. If you have further questions, please don't hesitate to contact us.

    Simeon Simeonov
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top