DeleteObject方法不会删除任何内容-请帮忙. [英] DeleteObject method doesn't delete anything - please help.

查看:84
本文介绍了DeleteObject方法不会删除任何内容-请帮忙.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.我正在尝试创建将从数据库中删除行的方法.
但是由于某种原因,当我启动程序时,什么也不会删除,也不会弹出错误.
我在db.Orders.DeleteObject(orderToDelete);做错什么了吗?



Hello. I am trying to create method that will delete rows from database.
But for some reason when i start program, nothing deletes and no errors popup.
Am i doing something wrong with db.Orders.DeleteObject(orderToDelete);?



Console.WriteLine("Please enter Order ID that you want to Delete");
try
{
    int choice3 = int.Parse(Console.ReadLine());
    var orderToDelete = (from c in db.Orders
                         where c.OrderID == choice3
                         select c).FirstOrDefault();

    if (orderToDelete != null)
    {
       db.Orders.DeleteObject(orderToDelete);
    }
    var orderExists = (from c in db.Orders
                       where c.OrderID == choice3
                       select c).FirstOrDefault();
    if (orderExists != null)
    {
        Console.WriteLine("Order not deleted.");
        DeleteFromDatabase();
    }
    else
    {
        Console.WriteLine("Order deleted.");
        MainMenu();
    }
}

推荐答案

您不会保存(提交)更改,以确保不会保存更改.如果您已阅读该文档,那么该文档中将包含示例代码:
You don''t save (commit) the changes so for sure they are not saved. If you would have read the documentation, you would have known as there is sample code in the documentation: ObjectContext.DeleteObject[^]

By the way, it was easy to find the answer with Google.


非常感谢.我很笨. :)
Thanks a lot. I am dumbass. :)


这篇关于DeleteObject方法不会删除任何内容-请帮忙.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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