none
Entity framework property load order RRS feed

  • Question

  • Hi all,

    I want to control the load order of properties when using entity framework. Is it possible? The reason is I want to control the setter of all properties with one property (the ReadOnly property). Tested the load order a few times and it seems like entity framework is loading the properties in the order they appear in code, i.e. if the ReadOnly propery is defined below the other properies the below code will work. Contrary: If it is defined above the other properties the entity will not be loaded correctly becauce entity framework first loads the ReadOnly property and then tries to set the others, which it can not becauce they are now "ReadOnly".

    The question is: Does anyone know whether this load order is just a coincidence or is the load order defined so I can use it as I intend?

     public class ProductFamily
        {
            private int _FamilyId;
            [Key, Column(Order = 1)]
            public int FamilyId {
                get {
                    return _FamilyId;
                }
                set {
                    if (!ReadOnly) {
                        _FamilyId = value;
                    }
                }
            }
    
            private string _Name;
            public string Name {
                get {
                    return _Name;
                }
                set {
                    if (!ReadOnly) {
                        _Name = value;
                    }
                }
            }
    
            protected bool _ReadOnly = false;
            public bool ReadOnly {
                get {
                    return _ReadOnly;
                }
                set {
                    if (!_ReadOnly) {
                        _ReadOnly = value;
                    }
                }
            }
    
            public ProductFamily() { }
    
        }

    Thank you,

    Best regards,

    Mike.


    • Edited by M Ward Tuesday, June 23, 2015 5:05 PM
    Tuesday, June 23, 2015 10:54 AM

Answers

  • You can differentiate between a setter called during EF object loading and after by setting a private field value on your object to indicate when loading is done.  Then setting that after materialization.

    eg

                using (var db = new MyDbContext())
                {
                    db.Database.Log = (s) => Console.WriteLine(s);
                    ((IObjectContextAdapter)db).ObjectContext.ObjectMaterialized += (s, a) =>
                    {
                        if (a.Entity is ProductFamily)
                        {
                            ((ProductFamily)a.Entity).SetLoaded();
                        }
                    };
    
    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by M Ward Wednesday, June 24, 2015 10:44 AM
    Tuesday, June 23, 2015 7:34 PM

All replies

  • You do that with custom objects that EF objects are mapped to and mapped back to the EF objects for persistence.
    Tuesday, June 23, 2015 6:31 PM
  • You can differentiate between a setter called during EF object loading and after by setting a private field value on your object to indicate when loading is done.  Then setting that after materialization.

    eg

                using (var db = new MyDbContext())
                {
                    db.Database.Log = (s) => Console.WriteLine(s);
                    ((IObjectContextAdapter)db).ObjectContext.ObjectMaterialized += (s, a) =>
                    {
                        if (a.Entity is ProductFamily)
                        {
                            ((ProductFamily)a.Entity).SetLoaded();
                        }
                    };
    
    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by M Ward Wednesday, June 24, 2015 10:44 AM
    Tuesday, June 23, 2015 7:34 PM