I'm new with ASP.net MVC. I wrote a simple program to save a new entry in a table. I have read a couple of answer for the same problem but none of them help me. When I add the new object and then execute the context.SaveChanges it gives me the error :
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I know nobody else is using this database. so that is not the problem. The code is show below.
On my table in the database which the key is "EntryID" is set up to auto increment. I know the EntryID is set to 0. but even if i change it manually in the view to any other number it still gives me the same error.
Any help would be very appreciated. Thanks
List.cshtml
@model MVC.Domain.Entities.InputList
@{
ViewBag.Title = "List";
}
@using(Html.BeginForm()) {
<p> Entry Number: @Html.TextBoxFor(x => x.EntryID)</p>
<p> Title : @Html.TextBoxFor(x => x.TItle)</p>
<p> Description : @Html.TextBoxFor(x => x.Description) </p>
<input type="submit" value = "Save" />
}
ProductController.cs
public class ProductController : Controller
{
private IProductRepository repository;
public ProductController(IProductRepository repoParam)
{
repository = repoParam;
}
public ViewResult List()
{
return View(new InputList());
}
[HttpPost]
public ActionResult List(InputList inputlist)
{
repository.SaveProduct(inputlist);
return RedirectToAction("List");
}
}
EFProductRepository.cs
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<InputList> InputList
{
get { return context.InputList; }
}
public void SaveProduct(InputList list)
{
context.InputList.Add(list);
context.SaveChanges();
}
}
public class EFDbContext : DbContext
{
public DbSet<InputList> InputList { get; set; }
}
InputLIst.cs
public class InputList
{
[Key]
public int EntryID { get; set; }
public string TItle { get; set; }
public string Description { get; set; }
}