0
votes

An error occurs when I call the function shown below:

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."

Function:

[HttpPost]
public ActionResult Index(InsertPo model)
{
        var context = new UsersContext();
        var po = new Po();
        var user = new User();
        po.PoId = 12;
        po.PoNumber = model.Po.PoNumber;
        po.Style = model.Po.Style;
        po.Quantity = model.Po.Quantity;
        po.Status = "hhh";
        po.OrderDate = Convert.ToDateTime("30-12-2011");
        po.ShipmentDate = Convert.ToDateTime("2-12-2011");
        po.ProductionRate = 10;
        po.UserId = 2;

        /*buyer.BuyerName = model.Buyer.BuyerName;*/
        /* buyer.BuyerId = 1;
         buyer.PoId = 10;*/
        context.Pos.Add(po);
        context.SaveChanges();
        return RedirectToAction("Index");
}
1
user Id=2 exists? , check key constraint. Have some trigger on Po table?. try po.User= new user(); - Xilmiki
Have you included a HiddenFor(model => model.Id) in your view? I've seen some issues when the model binder can't set the Id (or whatever you have as [Key]). - Theodoros Chatzigiannakis
Did you override any of the UsersContext methods? - haim770

1 Answers

0
votes

Try putting this line outside of your Action method.

    var context = new UsersContext();