user2255370
user2255370

Reputation:

Entity Framework data insert in foreign key

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

Upvotes: 0

Views: 832

Answers (1)

RealityDysfunction
RealityDysfunction

Reputation: 2639

Try putting this line outside of your Action method.

    var context = new UsersContext();

Upvotes: 0

Related Questions