Josh
Josh

Reputation: 1019

Remove method doesn't remove any items from the DB.

Let's see if I can make the step-by-step clear:

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

Which is executing the Remove method defined in 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);
        }
    }
}

But, I check my DB and no items are removed. Why is this? There may be some information missing, so let me know what else you require.

I'm having issues debugging this because I'm out of my element. If you have methods of debugging this, or certain things/keywords I can look up to help solve my issue, I would be thankful.

Upvotes: 1

Views: 78

Answers (2)

vikrantx
vikrantx

Reputation: 603

try this

public void Remove(int id)
        {
            InventoryItem item = context.Items.Find(id);
            context.Items.Remove(item);
            context.SaveChanges();
        }

Upvotes: 1

Alex
Alex

Reputation: 35409

Make sure you're committing the changes you make to Items against your DataContext:

Linq to SQL:

context.SubmitChanges();

Entity Framework:

context.SaveChanges();

Upvotes: 5

Related Questions