如何使用WPF之后的代码从本地数据库(SQLite)中删除数据?

时间:2019-03-20 01:58:51

标签: c# wpf sqlite

我想做一些类似的事情,例如当用户单击一个按钮时,它将从数据库中删除数据(每次用户运行系统时,它只有一个数据)。问题是我尝试使用此代码删除数据,但不起作用。

public void RemoveOrder(Order order)
    {
     try
        {

            using (tempPosOrderPaymentDBContext db = new tempPosOrderPaymentDBContext ())
            {

                db.Orders.Remove(order);
                db.SaveChanges();
            }


        }
        catch (Exception ex)
        {
            CustomExceptionHandling customExceptionHandling = new CustomExceptionHandling();
            customExceptionHandling.CustomExHandling(ex.ToString());
        }

}

当我尝试使用相同的代码将数据添加到数据库但仅将remove更改为Add时,它可以正常工作。但只有使用此删除项,它才行不通。

the <code>order</code> is not empty because when I put a breakdown it show exactly all the data inside the table

1 个答案:

答案 0 :(得分:1)

您可以尝试一下,它应该可以工作吗?

public void RemoveOrder(Order order)
{
 try
    {

        using (tempPosOrderPaymentDBContext db = new tempPosOrderPaymentDBContext ())
        {
            var orderInDb = db.Orders.First(x=> x.OrderId == order.OrderId);
            db.Orders.Remove(orderInDb);
            db.SaveChanges();
        }


    }
    catch (Exception ex)
    {
        CustomExceptionHandling customExceptionHandling = new CustomExceptionHandling();
        customExceptionHandling.CustomExHandling(ex.ToString());
    }

}