使用 Entity Framework 4 删除对象的最简单方法

发布于 2024-10-14 05:29:43 字数 796 浏览 3 评论 0原文

确认!我是实体框架的新手,正在尝试找到删除项目的最简单方法。

我有一个列表框,其数据源设置为数据库中的 TagCategory 对象。这工作正常。现在我想删除所选项目。所以我做了这样的事情:

TagCategory category = (TagCategory)lstCategories.SelectedItem;
using (MyEntities context = new MyEntities())
{
    context.AttachTo("TagCategories", category);
    context.DeleteObject(category);
    context.SaveChanges();
}

这看起来很简单,但它不起作用。没有删除任何内容,没有错误消息,什么也没有。

所以我发现我可以做这样的事情:

using (MyEntities context = new MyEntities())
{
    string cmd = String.Format("DELETE FROM TagCategory WHERE TagCatID=@ID",
        category.TagCatID));
    context.ExecuteStoreCommand(qry);
}

这似乎有效。那么我是只选择有效的方法,还是 Entity Framework 4 实际上能够做到这一点?

编辑:没关系。事实上,我还有另一个问题阻止了代码的执行。我发布的两个片段似乎都工作正常。抱歉。

Ack! I'm new to Entity Framework and am trying to find the simplest way to delete an item.

I have a listbox with the datasource set to TagCategory objects from the database. This is working fine. Now I'd like to delete the selected item. So I do something like this:

TagCategory category = (TagCategory)lstCategories.SelectedItem;
using (MyEntities context = new MyEntities())
{
    context.AttachTo("TagCategories", category);
    context.DeleteObject(category);
    context.SaveChanges();
}

This seems straight forward enough, but it doesn't work. Nothing is deleted, no error message, nothing.

So I see I can instead do something like this:

using (MyEntities context = new MyEntities())
{
    string cmd = String.Format("DELETE FROM TagCategory WHERE TagCatID=@ID",
        category.TagCatID));
    context.ExecuteStoreCommand(qry);
}

That seems to work. So do I just go with what works, or is Entity Framework 4 actually capable of doing this?

EDIT: Nevermind. In fact, I had another issue that prevented the code form executing. Both snippets I posted seem to work okay. My apologies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倚栏听风 2024-10-21 05:29:44

我不确定您是否可以使用 AttachTo() 来实现此目的。取决于您如何填充列表框。

什么应该起作用:

  var Key = context.CreateEntityKey("TagCategory", category);
  Object original;
  if (context.TryGetObjectByKey(Key, out original))
  {
        context.DeleteObject(original);
        context.SaveChanges();
  }

I'm not sure you can use AttachTo() for this. Depends on how you filled the ListBox.

What ought to work:

  var Key = context.CreateEntityKey("TagCategory", category);
  Object original;
  if (context.TryGetObjectByKey(Key, out original))
  {
        context.DeleteObject(original);
        context.SaveChanges();
  }
沉睡月亮 2024-10-21 05:29:44
// using the Find method of DBContext to retrieve the record you wish to delete
// works for me
// below code taken from a working WPF application using sdf database 

if (this.TransactionsDataGrid.SelectedIndex > -1)
{
    Transaction transaction = this.TransactionsDataGrid.SelectedItem as Transaction;
    if (transaction != null)
    {
        using (BilliEntities context = new BilliEntities())
        {
           try
           {
               Transaction trans = context.Transactions.Find(transaction.TransactionId);
               if (trans != null)
               {
                   // observable collection for datagrid
                   this.Transactions.Remove(transaction);
                   context.Transactions.Remove(trans);
                   context.SaveChanges();
               }
           }
           catch (Exception ex)
           {
              // for debugging
           }
        }
     }
 }
// using the Find method of DBContext to retrieve the record you wish to delete
// works for me
// below code taken from a working WPF application using sdf database 

if (this.TransactionsDataGrid.SelectedIndex > -1)
{
    Transaction transaction = this.TransactionsDataGrid.SelectedItem as Transaction;
    if (transaction != null)
    {
        using (BilliEntities context = new BilliEntities())
        {
           try
           {
               Transaction trans = context.Transactions.Find(transaction.TransactionId);
               if (trans != null)
               {
                   // observable collection for datagrid
                   this.Transactions.Remove(transaction);
                   context.Transactions.Remove(trans);
                   context.SaveChanges();
               }
           }
           catch (Exception ex)
           {
              // for debugging
           }
        }
     }
 }
高速公鹿 2024-10-21 05:29:43

您可以使用存根实体,如下所示:

using (var context = new MyEntities())
{
     var tagCategory = new TagCategory
     {
         PostId = category.TagCatID
     };
     context.TagCategories.Attach(tagCategory);
     context.DeleteObject(tagCategory);
     context.SaveChanges();
}

You can use stub entity, something like this:

using (var context = new MyEntities())
{
     var tagCategory = new TagCategory
     {
         PostId = category.TagCatID
     };
     context.TagCategories.Attach(tagCategory);
     context.DeleteObject(tagCategory);
     context.SaveChanges();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文