使用实体框架6 webform asp.net更新记录 [英] Update record using entity framework 6 webform asp.net

查看:58
本文介绍了使用实体框架6 webform asp.net更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试使用asp.net webform使用Entity framework 6更新我的记录。

我在实体框架中为Language类创建存储库所以我可以从外面打电话给他们。

这是我的代码:







我能做什么,我的代码有什么问题?



我尝试了什么:



public static bool更新(语言朗珑)

{

试试

{

使用(QuranDBEntities context = new QuranDBEntities())

{

语言_lang = context.Languages.Find(lang.LanguageID);

_lang = lang;

context.Languages.Attach(_lang);

context.SaveChanges();

}

返回true;

}

catch(Exception ex)

{

Auth.Document_Exception(ex);

返回false;

}

}





但代码是例外:

 System.ObjectDisposedException:ObjectContext实例已被释放,无法再使用进行需要连接的操作。









我用这段代码调用函数:

语言x; 
x = RepLanguage.Get(_langID);
x.Name = txt_Name.Text;
x.Lang = txt_Lang.Text;
x.IsActive = bool.Parse(ddl_IsActive.SelectedValue);
x.CreationDate = lbl_CreationDate.Text;
x.LastModifiedDate = DateTime.Now.ToShortDateString()+;
RepLanguage.Update(x);
}

解决方案

最后,我找到了解决方案。



就像我调用函数一样:



语言x; 
// ============== x = RepLanguage.Get( _langID);
x = new 语言();
x.LanguageID = int .Parse(txt_LanguageID.Text);
x.Name = txt_Name.Text;
x.Lang = txt_Lang.Text;
x.IsActive = bool .Parse(ddl_IsActive.SelectedValue);
x.CreationDate = lbl_CreationDate.Text;
x.LastModifiedDate = DateTime.Now.ToShortDateString()+& quot;& quot ;;
RepLanguage.Update(x);
} < / pre > < /跨度>


Hello,
I am trying to update my a record using Entity framework 6 with asp.net webform.
I create repository for the Language class in entity framework so I can call them from outside .
this is my code:



what can I do, and what is the problem with my code?

What I have tried:

public static bool Update(Language lang)
{
try
{
using(QuranDBEntities context = new QuranDBEntities())
{
Language _lang = context.Languages.Find(lang.LanguageID);
_lang = lang;
context.Languages.Attach(_lang);
context.SaveChanges();
}
return true;
}
catch(Exception ex)
{
Auth.Document_Exception(ex);
return false;
}
}


but the code through an exception :

System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.





I call the function using this code:

Language x;
                    x = RepLanguage.Get(_langID);
                    x.Name = txt_Name.Text;
                    x.Lang = txt_Lang.Text;
                    x.IsActive = bool.Parse(ddl_IsActive.SelectedValue);
                    x.CreationDate = lbl_CreationDate.Text;
                    x.LastModifiedDate = DateTime.Now.ToShortDateString() + "";
                    RepLanguage.Update(x);
                }

解决方案

Finally, I found the Solution.

it was in the way I call the function:

Language x;
                    // ============== x = RepLanguage.Get(_langID);
    x = new Language();
    x.LanguageID = int.Parse(txt_LanguageID.Text);
    x.Name = txt_Name.Text;
    x.Lang = txt_Lang.Text;
    x.IsActive = bool.Parse(ddl_IsActive.SelectedValue);
    x.CreationDate = lbl_CreationDate.Text;
    x.LastModifiedDate = DateTime.Now.ToShortDateString() + &quot;&quot;;
    RepLanguage.Update(x);
}</pre>


这篇关于使用实体框架6 webform asp.net更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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