Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hook object load event in Entity Framework?

Is there an EF equivalent to LINQ to SQL's OnCreated partial?

Several of my objects have XML fields that I would like to parse whenever the object is loaded from the db - I'd like to put the XML data into more friendly strongly-typed collections. I've already marked the XML field as private and hooked the SavingChanges event to re-build the XML before the item is committed back to the db, but I can't figure out how to populate the collections whenever the object is loaded.

I've thought of using the OnFieldChanged partial for my XML field, but that would run again whenever the XML field is re-built during SavingChanges, so it seems like there should be a better way.

like image 327
palmsey Avatar asked Oct 31 '25 00:10

palmsey


1 Answers

There is no OnLoaded event or similar as far as I know. A workaround might be to expose the collections as properties and lazily create/parse the values on first access:

private List<SomeData> _parsedDataCache;
public IList<SomeData> ParsedData {
    get {
        if (_parsedDataCache == null)
            ParseData();
        return _parsedDataCache;
    }
}
like image 54
David Schmitt Avatar answered Nov 03 '25 00:11

David Schmitt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!