c# - Remove 方法不会从数据库中删除任何项目。

标签 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);
        }
    }
}

正在执行 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:

Linq to SQL:

context.SubmitChanges();

Entity Framework:

context.SaveChanges();

关于c# - Remove 方法不会从数据库中删除任何项目。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11131412/

相关文章:

c# - Web 服务字符串中的 Null 被接收为 string.empty

asp.net-mvc - 用虚拟数据填充 POCO 的最佳方法是什么?

c# - "routes.LowercaseUrls = true;"不起作用?

c# - 无法从 Asp.Net WebApi 操作方法连接到网站 URL

asp.net - 您无权查看此目录或页面。 Azure Web API

c# - 如何将 Unity3d Mesh 保存到文件?

c# - ServiceStack.Redis 无法连接sPort

c# - .Net Framework 您必须添加对程序集 mscorlib 的引用,Version=4.0.0.0

c# - ASP.NET Web API 本地主机 :xxxx/api/product defaults to Home Page and not JSON data(orXML)

c# - 报告查看器 : How to load report as an embeddedresource in another assembly using reflection?