EF4 test if collection or related entity is proxy for self-tracking entities
All,
I'm starting out with Entity Framework 4 (not CTP5 yet) and am looking at the complexities of migrating NHibernate to EF.
Some code we have tests if a collection or related entity is a proxy and, if so, performs some different code to normal. If the collection is not a proxy (i.e. it has already been loaded into the context) then we do something else directly against the collection.
I've migrated this code to use the IRelatedEnd interface for collections, but the act of casting my I开发者_如何学PythonCollection to IRelatedEnd causes EF to load the collection... the result is that the collection is never a proxy when I check for IsLoaded == true in the next line.
Do I need to do something extra considering this is a custom POCO or is this expected behaviour?
Likewise, is there anyway to test if a related entity is a proxy or not?
Many thanks
The suggested method over on MSDN to test for proxies is to use something like this:
public static bool IsProxy(object type)
{
return type != null && ObjectContext.GetObjectType(type.GetType()) != type.GetType();
}
This method works in both EF 4 and EF 4.1 Codefirst.
As for the lazy loading, I've never use the IRelatedEnd - just left the navigation properties in the POCO's as ICollection and ensure that lazy loading is enabled. Beyond that, it just works.
context.ContextOptions.LazyLoadingEnabled = true;
There's also an option in the edmx for lazy loading.
精彩评论