在 MVC 中更新 Master / Details
Updating Master / Details in MVC
我正在尝试更新主/详细信息关系。目前我的代码只更新主 table 但不会向详细信息页面添加任何内容。
我可以将记录添加到两个 tables 中,但我希望能够同时更新母版并向详细信息中添加新条目。
屏幕截图显示了传递到我的控制器的数据。
[HttpPost]
public JsonResult saveEdit(OMR_OrderMaster order)
{
bool status = false;
{
using (PPPEntities dc = new PPPEntities())
{
{
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
status = true;
}
}
}
return new JsonResult { Data = new { status = status } };
}
目前没有返回任何错误,但正在更新主表
namespace PPP2.Models
{
using System;
using System.Collections.Generic;
public partial class OMR_OrderMaster
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public OMR_OrderMaster()
{
this.OMR_OrderDetails = new HashSet<OMR_OrderDetails>();
}
public int OrderID { get; set; }
public string OrderNo { get; set; }
public System.DateTime OrderDate { get; set; }
public string Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<OMR_OrderDetails> OMR_OrderDetails { get; set; }
}
}
namespace PPP2.Models
{
using System;
using System.Collections.Generic;
public partial class OMR_OrderDetails
{
public int OrderDetialsID { get; set; }
public int OrderID { get; set; }
public decimal Rate { get; set; }
public int Quantity { get; set; }
public int StockID { get; set; }
public virtual OMR_OrderMaster OMR_OrderMaster { get; set; }
}
}
因为您处于 EDMX 上下文中,所以您必须使用 Attach
函数,例如:
using (PPPEntities dc = new PPPEntities())
{
foreach(var details in order.OMR_OrderDetails)
dc.ORM_OrderDetails.Attach(details);
dc.Entry(order).State = EntityState.Modified;
dc.SaveChanges();
status = true;
}
Be careful in your code you are declaring a PPPEntities
context but you don't use it. Your code is calling an undefined db
var.
使用 Attach 抛出错误,但更改为 add 在我的情况下有效
所以,谢谢你让我走上正轨
foreach (var details in order.OMR_OrderDetails)
dc.OMR_OrderDetails.Add(details);
dc.Entry(order).State = EntityState.Modified;
dc.SaveChanges();
status = true;
我正在尝试更新主/详细信息关系。目前我的代码只更新主 table 但不会向详细信息页面添加任何内容。 我可以将记录添加到两个 tables 中,但我希望能够同时更新母版并向详细信息中添加新条目。 屏幕截图显示了传递到我的控制器的数据。
[HttpPost]
public JsonResult saveEdit(OMR_OrderMaster order)
{
bool status = false;
{
using (PPPEntities dc = new PPPEntities())
{
{
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
status = true;
}
}
}
return new JsonResult { Data = new { status = status } };
}
目前没有返回任何错误,但正在更新主表
namespace PPP2.Models
{
using System;
using System.Collections.Generic;
public partial class OMR_OrderMaster
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public OMR_OrderMaster()
{
this.OMR_OrderDetails = new HashSet<OMR_OrderDetails>();
}
public int OrderID { get; set; }
public string OrderNo { get; set; }
public System.DateTime OrderDate { get; set; }
public string Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<OMR_OrderDetails> OMR_OrderDetails { get; set; }
}
}
namespace PPP2.Models
{
using System;
using System.Collections.Generic;
public partial class OMR_OrderDetails
{
public int OrderDetialsID { get; set; }
public int OrderID { get; set; }
public decimal Rate { get; set; }
public int Quantity { get; set; }
public int StockID { get; set; }
public virtual OMR_OrderMaster OMR_OrderMaster { get; set; }
}
}
因为您处于 EDMX 上下文中,所以您必须使用 Attach
函数,例如:
using (PPPEntities dc = new PPPEntities())
{
foreach(var details in order.OMR_OrderDetails)
dc.ORM_OrderDetails.Attach(details);
dc.Entry(order).State = EntityState.Modified;
dc.SaveChanges();
status = true;
}
Be careful in your code you are declaring a
PPPEntities
context but you don't use it. Your code is calling an undefineddb
var.
使用 Attach 抛出错误,但更改为 add 在我的情况下有效 所以,谢谢你让我走上正轨
foreach (var details in order.OMR_OrderDetails)
dc.OMR_OrderDetails.Add(details);
dc.Entry(order).State = EntityState.Modified;
dc.SaveChanges();
status = true;