我正在尝试更新主/详细信息关系。目前,我的代码只更新了主表,但没有向详细信息页面添加任何内容。 我可以很好地向两个表添加记录,但我希望能够更新主服务器并同时向细节添加新条目。 屏幕截图显示了传递给我的Controller的数据。
[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; }
}
}
答案 0 :(得分:0)
因为您处于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;
}
在您的代码中要小心宣布
PPPEntities
上下文,但不要使用它。您的代码正在调用未定义的db
var。
答案 1 :(得分:0)
使用Attach引发了错误,但更改为添加在我的情况下工作 所以,谢谢你让我走上正轨
AppDatabase.getInstance(getActivity()).mediaDao()
.getMediaByPathTest(getCurrentMedia().getPath())
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(media -> {
if (media != null && media.isFavourite()) {
Toast.makeText(SingleMediaActivity.this,
"Bingo I m favorite", Toast.LENGTH_SHORT).show();
}
}, throwable -> {
}
, () -> {
});