实体框架数据插入外键

时间:2013-06-02 07:47:40

标签: asp.net-mvc entity-framework asp.net-mvc-4

当我调用下面显示的函数时发生错误:

  

存储更新,插入或删除语句会影响意外的行数(0)。自实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目。“

功能:

[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 个答案:

答案 0 :(得分:0)

尝试将此行放在Action方法之外。

    var context = new UsersContext();