I have a normalised database that I’m using with EF4.3.
In this instance, I have a User (everyone is a user). Some are Subscribers and Subscribers are either Contributors, Members or Administrators.
So for each type of User, I include the appropriate associations via navigation properties.
So a Member linq statement would be something like this:
var u = r.FindBy(x => x.UserId == userId)
.Include("Subscribers")
.Include("Members")
.SingleOrDefault();
In terms of an object everyone is a User – just different permutations.
I’m trying to fing the best way of handling this in code when some of the navigation properties aren’t included. For instance, I wouldn’t include Contributor or Administrator in the example above. Normally I could check for user.subscriber.contributor not being NULL, but instead I get the “object context has been disposed of…” even when I make this check.
How do I work around this?
You are receiving exception because your entities are proxied for lazy loading and they were not correctly detached before you disposed the context. Detaching entities with loaded relations is not directly possible (you must create a deep copy through serizliazation instead) so the best solution in this case is turning lazy loading off. In such case your entity will not try to lazy load navigation properties which are accessed for the first time.
You can also turn off whole proxy creation instead: