自定义属性:得是很明显的东西我做错了 [英] Custom property: gotta be something obvious I'm doing wrong

查看:122
本文介绍了自定义属性:得是很明显的东西我做错了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经加入我的不同实体的部分类添加各种有用的方法没有问题。

试图添加的属性出现的基础上,我已经看到的例子是简单的,但我都惨遭失败。

更新例如:

 公开名单<&朋友GT; FriendsInGoodStanding
    {
        得到
        {
            使用(VAR上下文=新GarbageEntities())
            {
                VAR一个= context.Friends.Include(aspnet_User1),其中(F => f.UserID == this.UserId&放大器;&安培; f.Blocked ==假)。.ToList();
                变种B = context.Friends.Include(aspnet_User),其中(F => f.FriendUserID == this.UserId&放大器;&安培; f.Blocked ==假)。.ToList();
                a.AddRange(二);
                返回a.Distinct()了ToList()。
            }
        }
    }

我收到以下错误任何时候我尝试使用这个属性:


  

该ObjectContext的实例已
  布置,并且可以不再被用于
  需要连接操作。


  4936线:获得
4937行:{
4938线:回归((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<aspnet_User>(\"GarbageModel.FK_Friends_aspnet_Users\", aspnet_User)值。
4939行:}

这一定有什么明显的,我已经忽略了。

解决方案

这个错误的根源是因为你的程序正在试图延迟加载你的朋友实体对象的导航属性之一,当您已经阅读出现这种情况在 FriendsInGoodStanding 的属性和ObjectContext的已被释放,因为using语句。
结果
现在,我可以看到你渴望加载aspnet_User1,也有你调用了ToList()在查询的末尾,以便它必须是的朋友的对象上的另一个导航属性​​。如果显示,使用客户端code中的的 FriendsInGoodStanding 的属性,则我可以确切地告诉其中之一是,但现在我的预感是,你有一个朋友的对象名为aspnet_User财产也需要急于负载是这样的:

 公共部分类aspnet_User {    公开名单FriendsInGoodStanding {
        获得{
            使用(VAR上下文=新GarbageEntities()){                VAR一个= context.Friends
                     .INCLUDE(aspnet_User1)
                     .INCLUDE(aspnet_User)
                     。凡(F => f.UserID == this.UserId && f.Blocked ==假).ToList();                变种B = context.Friends
                     .INCLUDE(aspnet_User)
                     .INCLUDE(aspnet_User1)
                     。凡(F => f.FriendUserID == this.UserId && f.Blocked ==假).ToList();
                a.AddRange(二);
                返回a.Distinct()了ToList()。
            }
        }
    }
}

搜索结果
另一个解决方案:
结果
停用延迟加载作为您的对象上下文来克服这个例外。您可以通过在模型上右击做到这一点,然后选择属性,然后找到延迟加载已启用选项,默认为真,只需将它设置为false。或者通过编程可以code:

  VAR背景=新GarbageEntities();
context.ContextOptions.LazyLoadingEnabled = FALSE;

一旦被禁止,你仍然可以明确,如果需要按需加载相关数据,或
即使加载数据的初始查询一起。
刚才看出来的NullReferenceException!

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:

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!

这篇关于自定义属性:得是很明显的东西我做错了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆