解决方案---此 ObjectContext 实例已释放,不可再用于需要连接的操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aa2012123456/article/details/52224855

linq一对多关联查询,结果报错,如下:

错误的代码:

 using (var DbContext = new MyDbcontext())
            {
                var users = DbContext.User.Include(x => x.DepartMent);
                var query = (from u in DbContext.User
                             join d in DbContext.DepartMent
                                on u.DepartMent.DeptId equals d.DeptId
                            select u).ToList<User>();
                return query;
            }
正确的代码:查询的时候包含另一张表就可以了,自己煞笔了!

IQueryable<User> user = DbContext.User.Include(x => x.DepartMent);
                if (!string.IsNullOrEmpty(userid))
                {
                    user = user.Where(o => o.UserID == userid);
                }
                if (!string.IsNullOrEmpty(deptid))
                {
                    user = user.Where(u => u.DepartMent.DeptId == deptid);
                }
                return user.ToList();



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

04-17

[code=csharp]rn public partial class Usersrn rn public users()rn rn this.advanced_property = new HashSet();rn this.secondary_property = new HashSet();rn rn rn public int id get; set; rn public string qq get; set; rn public string sex get; set; rn public string ages get; set; rn public string zodiac_signs get; set; rn public string constellation get; set; rn public string blood_type get; set; rn public string location get; set; rn rn public virtual ICollection advanced_property get; set; rn public virtual ICollection secondary_property get; set; rn rnrnpublic List FindListByExpression(Expression> where, string greedyLoad = "")rn rn tryrn rnrn using (context = new qqMarketingEntities1())rn rn var query = context.Set();rn if (!string.IsNullOrEmpty(greedyLoad))rn rn List greedyLoads = greedyLoad.Split(',').ToList();rn foreach (var i in greedyLoads)rn rn query.Include(i);rn rn query.Where(where);rn rnrn List result = query.ToList();rn return result;rn rn rn catch (Exception e)rn rn throw e;rn rnrn rnrnpublic List GetUsersByParam(List qqList, string sex = "", string ages = "", string zodica = "", string constellation = "", string blood = "", string location = "", string greedyLoad = "")rn rn var esp = DynamicLinqExpressions.True();rn esp.And(o => qqList.Contains(o.qq));rn if (!string.IsNullOrEmpty(sex))rn esp = esp.And(o => o.sex.Equals(sex));rn if (!string.IsNullOrEmpty(ages))rn esp = esp.And(o => o.ages.Equals(ages));rn if (!string.IsNullOrEmpty(zodica))rn esp = esp.And(o => o.zodiac_signs.Equals(zodica));rn if (!string.IsNullOrEmpty(constellation))rn esp = esp.And(o => o.constellation.Equals(constellation));rn if (!string.IsNullOrEmpty(blood))rn esp = esp.And(o => o.blood_type.Equals(blood));rn if (!string.IsNullOrEmpty(location))rn esp = esp.And(o => o.sex.Equals(location));rn AbstraceDao dao = new AbstraceDao();rn List result = dao.FindListByExpression(esp,greedyLoad);rnrn return result;rn rn[/code]rnrn UserDao dao = new UserDao();rn List result = dao.GetUsersByParam(qqList, sex, ages, zodica, constellation, blood, location, "advanced_property,secondary_property");rnrn通过断点进入得知 获取的数据是正常的,删除断点的情况下rn获取advanced_property时依然会出现"此 ObjectContext 实例已释放,不可再用于需要连接的操作" 的错误 论坛

没有更多推荐了,返回首页