如何在Entity Framework中删除 [英] How to delete in Entity Framework

查看:97
本文介绍了如何在Entity Framework中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这个但是有一个问题..我知道我错过了一个非常重要的部分..我只是一个初学者所以我不是很擅长这个。如果有人可以帮助我,我会很高兴。







  protected   void  OnRowDeleting( object  sender,GridViewDeleteEventArgs e)
{


尝试
{

tblFee objtblFee = tblFee();


int index = Convert.ToInt32(e.RowIndex);


objdbPollyClinicEntities.tblFees.DeleteObject(objtblFee);

objdbPollyClinicEntities.SaveChanges();

populateGrd();

lblMsg.Text = 费用已成功删除。;

}

catch (例外情况)
{

lblMsg .Text = ex.Message;
}











在aspx页面中我做了:

< asp:GridView ID =   grdShow runat =   server AutoGenerateColumns =   true AllowSorting =   true AllowPaging =   false 
AutoGenerateDeleteButton = true OnRowDeleting = OnRowDeleting>

解决方案

  protected   void  OnRowDeleting( object  sender) ,GridViewDeleteEventArgs e)
{


try
{
int index = Convert.ToInt32(e.RowIndex);
tblFee objtblFee = objdbPollyClinicEntities.tblFees.First(t => t。 yourprimarykey == index)

objdbPollyClinicEntities.tblFees.DeleteObject(objtblFee);

objdbPollyClinicEntities.SaveChanges();

populateGrd();

lblMsg.Text =& amp; quot;费用已成功删除。& amp; quot ;;

}

catch (例外情况)
{

lblMsg .Text = ex.Message;
}& lt; / pre& gt; < / pre >


I tried this but there's a problem.. I know I have missed out a very important part.. I'm just a beginner so I'm not so good at this. I'd be glad if someone could help me out.



protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
       {


          try
           {

               tblFee objtblFee = new tblFee();


               int index = Convert.ToInt32(e.RowIndex);


               objdbPollyClinicEntities.tblFees.DeleteObject(objtblFee);

               objdbPollyClinicEntities.SaveChanges();

               populateGrd();

                lblMsg.Text = "Fee successfully deleted.";

           }

           catch (Exception ex)
          {

               lblMsg.Text = ex.Message;
           }






In aspx page I did:

<asp:GridView ID="grdShow" runat="server" AutoGenerateColumns="true" AllowSorting="true" AllowPaging="false"
      AutoGenerateDeleteButton="true" OnRowDeleting="OnRowDeleting" >

解决方案

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
       {


          try
           {
               int index = Convert.ToInt32(e.RowIndex);
               tblFee objtblFee =objdbPollyClinicEntities.tblFees.First(t=>t.yourprimarykey==index)

               objdbPollyClinicEntities.tblFees.DeleteObject(objtblFee);

               objdbPollyClinicEntities.SaveChanges();

               populateGrd();

                lblMsg.Text = &amp;quot;Fee successfully deleted.&amp;quot;;

           }

           catch (Exception ex)
          {

               lblMsg.Text = ex.Message;
           }&lt;/pre&gt;</pre>


这篇关于如何在Entity Framework中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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