views:

72

answers:

1

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.

+2  A: 
Morteza Manavi
I will double check on this as soon as I put out the smoldering lasagna in my oven. Thanks.
PolishedTurd
If you could not find the missed nav property, just post your client code and we'll go from there.
Morteza Manavi
I appreciate all your help. This is no longer generating an exception, but I have no idea why I have to query for both navigation properties when I have no need of them. Am I just using the wrong query altogether? Is there any easier way? Thanks.
PolishedTurd
Disabling lazy loading is generally a good idea for web applications?
PolishedTurd
No problem. Well, if you don't need them both then you should refactor your client code and remove the unnecessary calls to aspnet_User1 and aspnet_User nav properties since at some point you are calling them both and because they are not loaded, EF tries to lazy load them and it fails (they might even being called from your MVC View pages so you need to inspect all layers of your application) the easier (not the better) way would be my 2nd solution by disabling Lazy Loading on your context. It will probably cause NullReferenceException though.
Morteza Manavi
I'm guessing there must be some place in my code that is looking for that navigation property that I don't know about...
PolishedTurd
Yes, it's a good practice "in general" for WCF and Web applications to always disable Lazy Loading. Like I said, Once it is disabled, you can still explicitly load related data on demand if needed, or eager load them with the initial query. In your case I would say you should first refactor your code and then disable lazy loading.
Morteza Manavi