删除方法不会从数据库中删除任何项目.

Jos*_*osh 1 c# asp.net-mvc asp.net-web-api

让我们看看我是否可以逐步明确:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是执行以下Remove定义的方法InventoryRepository:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我检查我的数据库,没有删除任何项目.为什么是这样?可能缺少某些信息,请告诉我您还需要什么.

我在调试这个问题是因为我不在我的元素中.如果您有调试方法,或者我可以查找某些事情/关键字以帮助解决我的问题,我将非常感激.

xan*_*ded 5

请确保您提交您对改变Items对你DataContext:

Linq to SQL:

context.SubmitChanges();
Run Code Online (Sandbox Code Playgroud)

实体框架:

context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)