entity-frameworklazy-loadingnavigation-properties

Problems lazy loading during IValidatableObject's Validate method


I'm using EF4.3, POCOs, Lazy Loading and the IValidatableObject interface.

I have a loan entity with a 1-1 relationship with a contract entity. Contract contains a bunch of data that is best left unloaded unless required.

My problem is... When I make changes to my loan entity, it's Validate method is meant to load the related contract entity to ensure it's data is valid according to the state (or status) of the loan... but the contract navigational property remains null? If I quickwatch the loan immediately before SaveChanges is called, the contract data is loaded and SaveChanges proceeds fine, otherwise it fails.

If someone is able to explain why I am seeing this behavior, I would be most grateful.

Cheers

Craig


Solution

  • Lazy loading is turned off by design (internal EF implementation) during validation to avoid unexpected queries to database. If your validation logic requires related entity your code should load it.