让我们看看我是否可以把步骤讲清楚:
namespace InventoryManager.Controllers
{
public class InventoryController : ApiController
{
private readonly IInventoryRepository repository;
//...
public HttpResponseMessage DeleteItem(int id)
{
// Executes fine
repository.Remove(id);
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
}
正在执行 InventoryRepository
中定义的 Remove
方法:
namespace InventoryManager.Models
{
public class InventoryRepository : IInventoryRepository
{
private InventoryContext context;
//...
public void Remove(int id)
{
InventoryItem item = context.Items.Find(id);
// Executes fine
context.Items.Remove(item);
}
}
}
但是,我检查了我的数据库,没有任何项目被删除。这是为什么?可能缺少一些信息,所以让我知道您还需要什么。
我在调试时遇到问题,因为我不适应。如果您有调试方法,或者我可以查找某些东西/关键字来帮助解决我的问题,我将不胜感激。
最佳答案
确保您将对 Items
所做的更改提交给您的 DataContext
:
context.SubmitChanges();
context.SaveChanges();
关于c# - Remove 方法不会从数据库中删除任何项目。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11131412/