c# - Check if navigational property is loaded -


is possible check if navigational property has been loaded? when try access objectdisposedexception

the objectcontext instance has been disposed , can no longer used operations require connection.

public class factory {     // ..     public virtual icollection<machine> machines { get; set; } }  // ... ilist<factory> set; using (mycontext context = new mycontext()) {     set = context.factories.tolist(); }  ... later on  // possible check if .machines loaded here? if (set.first().machines == loaded)      // let's open new context , load } 

the solution have found when searching use include() , include machines in first run, avoid loading until neccessary. tried box in new using(...){} still exception.
i'd avoid attach since extremely slow when building large object graphs.

i guess use bool ismachinesloaded or something, figured there ought way check without that..

i solved method:

protected bool isloaded<tentity>(tentity entity, func<tentity, object> testaction) {     try     {         testaction(entity);         return true;     }     catch (objectdisposedexception)     {         return false;     } } 

which called via:

if (isloaded(myentity, entity => entity.mylist)){     // navigational property exists , can accesses } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -