I have been adding partial classes of my different entities to add various useful methods without issue.
Attempting to add properties appears to be straightforward based on examples that I've seen, but mine are failing miserably.
Updated example:
public List<Friend> FriendsInGoodStanding
{
get
{
using (var context = new GarbageEntities())
{
var a = context.Friends.Include("aspnet_User1").Where(f => f.UserID == this.UserId && f.Blocked == false).ToList();
var b = context.Friends.Include("aspnet_User").Where(f => f.FriendUserID == this.UserId && f.Blocked == false).ToList();
a.AddRange(b);
return a.Distinct().ToList();
}
}
}
I am receiving the following error any time I attempt to make use of this property:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Line 4936: get
Line 4937: {
Line 4938: return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<aspnet_User>("GarbageModel.FK_Friends_aspnet_Users", "aspnet_User").Value;
Line 4939: }
This must be something obvious that I've overlooked.
The source of this error is because your program is trying to "Lazy Load" one of the navigation properties on your Friend entity object and this happens when you already read the FriendsInGoodStanding property and the objectcontext has been disposed, because of the using statement.
Now, I can see that you are eager loading "aspnet_User1" and also you are calling ToList() at the end of your query so it must be another navigation property on the Friend object. If you show the client code that uses the FriendsInGoodStanding property then I can exactly tell which one is that but for now my hunch is that you have a property named aspnet_User on Friend object which also needs to be eager load like this:
Another Solution:
would be to disable lazy loading for your object context to get over with this exception. You can do so by right clicking on your model and then selecting properties and then find "Lazy Loading Enabled" option which is true by default, just set it to false. Or programmatically you can code:
Once it is disabled, you can still explicitly load related data on demand if needed, or even load the data along with the initial query. Just watch out for NullReferenceException!