首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

运用 Include 后 依然此 ObjectContext 实例已释放,不可再用于需要连接的操作

2013-04-20 
使用 Include后 依然此 ObjectContext 实例已释放,不可再用于需要连接的操作public partial class Users{p

使用 Include 后 依然此 ObjectContext 实例已释放,不可再用于需要连接的操作


    public partial class Users
    {
        public users()
        {
            this.advanced_property = new HashSet<advanced_property>();
            this.secondary_property = new HashSet<secondary_property>();
        }
    
        public int id { get; set; }
        public string qq { get; set; }
        public string sex { get; set; }
        public string ages { get; set; }
        public string zodiac_signs { get; set; }
        public string constellation { get; set; }
        public string blood_type { get; set; }
        public string location { get; set; }
    
        public virtual ICollection<advanced_property> advanced_property { get; set; }
        public virtual ICollection<secondary_property> secondary_property { get; set; }
    }

public List<T> FindListByExpression(Expression<Func<T, bool>> where, string greedyLoad = "")
        {
            try
            {

                using (context = new qqMarketingEntities1())
                {
                    var query = context.Set<T>();
                    if (!string.IsNullOrEmpty(greedyLoad))
                    {
                        List<string> greedyLoads = greedyLoad.Split(',').ToList();
                        foreach (var i in greedyLoads)
                        {
                            query.Include(i);
                        }


                        query.Where(where);
                    }

                    List<T> result = query.ToList<T>();
                    return result;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

        }

public List<Users> GetUsersByParam(List<string> qqList, string sex = "", string ages = "", string zodica = "", string constellation = "", string blood = "", string location = "", string greedyLoad = "")
        {
            var esp = DynamicLinqExpressions.True<users>();
            esp.And(o => qqList.Contains(o.qq));
            if (!string.IsNullOrEmpty(sex))
                esp = esp.And(o => o.sex.Equals(sex));
            if (!string.IsNullOrEmpty(ages))
                esp = esp.And(o => o.ages.Equals(ages));
            if (!string.IsNullOrEmpty(zodica))
                esp = esp.And(o => o.zodiac_signs.Equals(zodica));
            if (!string.IsNullOrEmpty(constellation))
                esp = esp.And(o => o.constellation.Equals(constellation));
            if (!string.IsNullOrEmpty(blood))
                esp = esp.And(o => o.blood_type.Equals(blood));
            if (!string.IsNullOrEmpty(location))
                esp = esp.And(o => o.sex.Equals(location));
            AbstraceDao<users> dao = new AbstraceDao<users>();
            List<users> result = dao.FindListByExpression(esp,greedyLoad);

            return result;


        }



 UserDao dao = new UserDao();
            List<Users> result = dao.GetUsersByParam(qqList, sex, ages, zodica, constellation, blood, location, "advanced_property,secondary_property");

通过断点进入得知 获取的数据是正常的,删除断点的情况下
获取advanced_property时依然会出现"此 ObjectContext 实例已释放,不可再用于需要连接的操作" 的错误
[解决办法]
禁用Context的Deferred Query Evaluation,然后在查询结束后手动加载所有需要的导航属性
http://www.cnblogs.com/dabaopku/archive/2011/07/16/2108351.html

热点排行