0

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; }        
}
CodeEngine
  • 296
  • 1
  • 9
  • 28
  • Possible duplicate of [this] (http://stackoverflow.com/questions/1836173/entity-framework-store-update-insert-or-delete-statement-affected-an-unexpec?rq=1) – Peter Smith May 13 '15 at 14:37
  • 2
    If you have the primary key set up as auto-increment, you shouldn't be populating the `EntryId` property. – markpsmith May 13 '15 at 14:38
  • Can you show your InputList object? – Carl May 13 '15 at 14:45
  • @Carl I edited my question. Is that what you were asking for. – CodeEngine May 13 '15 at 14:47
  • @markpsmith That's what I thought. so I tried without populating the EntryId but still got the same error. Thanks for the time guys any other suggestions? – CodeEngine May 13 '15 at 14:49
  • @omachu23 - couple of things you could try - first, if you have SQL Profiler, see what is actually being executed against the database, to ensure that the INSERT statement is running correctly. Second - and I'm grasping at straws here - move the instantiation of your EFDbContext to a constructor in your repository. – Carl May 13 '15 at 14:55
  • Try removing `@Html.TextBoxFor(x => x.EntryID)` from the form. – markpsmith May 13 '15 at 16:12
  • @Carl I tried moving the instantiation of my EFDContext to a constructor and did not work. And Im waiting to be given sysadmin to the db so i can use SQL server Profle – CodeEngine May 13 '15 at 18:07
  • @markpsmith I tried what you suggested but it didn't work. – CodeEngine May 13 '15 at 18:08

0 Answers0