views:

423

answers:

2

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.

+1  A: 

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;
    }
}
David Schmitt
A: 

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

Baldy