Hook object loading event in Entity Framework?

Is there an EF equivalent for LINQ to SQL OnCreated partial?

Some of my objects have XML fields that I would like to parse whenever the object is loaded from db. I would like to put the XML data into more friendly strongly typed collections. I already marked the XML field as private and hooked on the SavingChanges event to rebuild the XML before the element is passed back to db, but I cannot figure out how to populate the collection whenever the object is loaded.

I was thinking about using a partial OnFieldChanged field for my XML field, but this will work again whenever the XML field is rebuilt during SavingChanges, so there seems to be a better way.

+3
source share
3 answers

There is no OnLoaded event or the like, as far as I know. A workaround may be to expose collections as properties and lazily create / analyze values ​​on first access:

 private List<SomeData> _parsedDataCache; public IList<SomeData> ParsedData { get { if (_parsedDataCache == null) ParseData(); return _parsedDataCache; } } 
+2
source

Or use the ObjectContext.ObjectMaterialized event in .net 4.0

+3
source

You must create a partial class (as in LINQ to SQL) and just use the default constructor.

-2
source

All Articles