Custom property: gotta be something obvious I'

2019-07-15 00:14发布

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.

1条回答
别忘想泡老子
2楼-- · 2019-07-15 00:35

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:

public partial class aspnet_User{

    public List FriendsInGoodStanding {
        get {
            using (var context = new GarbageEntities()) {

                var a = context.Friends
                     .Include("aspnet_User1")
                     .Include("aspnet_User")
                     .Where(f => f.UserID == this.UserId && f.Blocked == false).ToList();

                var b = context.Friends
                     .Include("aspnet_User")
                     .Include("aspnet_User1")
                     .Where(f => f.FriendUserID == this.UserId && f.Blocked == false).ToList();
                a.AddRange(b);
                return a.Distinct().ToList();
            }
        }
    }
}



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:

var context = new GarbageEntities();
context.ContextOptions.LazyLoadingEnabled = false;

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!

查看更多
登录 后发表回答