解决“ObjectContext实例已被释放,不能再用于需要连接的操作我正在尝试植入一个GridView使用EntityFrameworkm,但每次收到以下错误时:“对象‘Cosis_DAL.MemberLoan’上的属性访问器‘LoanProduct’抛出了以下异常:ObjectContext实例已被释放,不能再用于需要连接的操作。”我的代码是:public List<MemberLoan> GetAllMembersForLoan(string keyword){
using (CosisEntities db = new CosisEntities())
{
IQueryable<MemberLoan> query = db.MemberLoans.OrderByDescending(m => m.LoanDate);
if (!string.IsNullOrEmpty(keyword))
{
keyword = keyword.ToLower();
query = query.Where(m =>
m.LoanProviderCode.Contains(keyword)
|| m.MemNo.Contains(keyword)
|| (!string.IsNullOrEmpty(m.LoanProduct.LoanProductName) && m.LoanProduct.LoanProductName.ToLower().Contains(keyword))
|| m.Membership.MemName.Contains(keyword)
|| m.GeneralMasterInformation.Description.Contains(keyword)
);
}
return query.ToList();
}}protected void btnSearch_Click(object sender, ImageClickEventArgs e){
string keyword = txtKeyword.Text.ToLower();
LoanController c = new LoanController();
List<COSIS_DAL.MemberLoan> list = new List<COSIS_DAL.MemberLoan>();
list = c.GetAllMembersForLoan(keyword);
if (list.Count <= 0)
{
lblMsg.Text = "No Records Found";
GridView1.DataSourceID = null;
GridView1.DataSource = null;
GridView1.DataBind();
}
else
{
lblMsg.Text = "";
GridView1.DataSourceID = null;
GridView1.DataSource = list;
GridView1.DataBind();
}}错误是提到了LoanProductName列的Gridview..提到:我正在使用C#,ASP.NET,SQLServer 2008作为后端DB。我对实体框架非常陌生。我不明白我为什么要犯这个错误。有人能帮我吗?
3 回答
- 3 回答
- 0 关注
- 2023 浏览
添加回答
举报
0/150
提交
取消