我在模型绑定器创建模型,子孙后代然后使用context.SaveChanges();
时尝试将对象插入子集合时收到以下错误违反了多重性约束。角色' OrderDetail_OrderDetailPricelistProductOptions_Source'关系' PPLib.Models.OrderDetail_OrderDetailPricelistProductOptions'具有多重性1或0..1。
我的模型如下(为简洁起见,删除了属性);
public class Order
{
public int OrderId { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
}
public class OrderDetail
{
public int OrderDetailId { get; set; }
public int OrderId { get; set; }
public int ProductId { get; set; }
public virtual Product Product { get; set; } //FK NAV
public int? PricelistProductId { get; set; } // if a subscriber order ...has the ProductId from a PriceList.
private decimal _Price = 0;
public decimal Price { get { return _Price; } set { _Price = value; } }
private int _Quantity = 1;
public int Quantity { get { return _Quantity; } set { _Quantity = value; } }
public virtual List<OrderDetailPricelistProductOption> OrderDetailPricelistProductOptions { get; set; }
}
public class OrderDetailPricelistProductOption
{
public int OrderDetailPricelistProductOptionId { get; set; }
public int OrderDetailId { get; set; }
public virtual List<OrderDetailPricelistProductOptionsDetail> OrderDetailPricelistProductOptionsDetails { get; set; }
}
public class OrderDetailPricelistProductOptionsDetail
{
public int OrderDetailPricelistProductOptionsDetailId { get; set; }
public int OrderDetailPricelistProductOptionId { get; set; }
public string Name { get; set; }
}
更清楚:
如果我提交一份完整的新订单,其中包含OrderDetails列表,OrderDetailPricelistProductOptions列表及其OrderDetailPricelistProductOptionsDetails列表,则模型绑定器会完成其工作,并且我没有收到任何错误:
db.Orders.Add(order);
db.SaveChanges();
如果我提交一个Edit with和Existing Order以及一个Orderdetails列表,它的OrderDetailPricelistProductOptions列表及其OrderDetailPricelistProductOptionsDetails列表,我从DB上下文中获取Order,然后使用以下方法合并来自视图模型的OrderDetails:
order.OrderDetails.AddRange(pricelistProductVM.Order.OrderDetails);
我没有收到任何错误:
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
我有一个特殊情况,我必须实例化一个名为autoFillOd的新OrderDetail,并从Model Binder组装的现有OrderDetails中注入其值。我更改其Quantity值,然后将其添加到ViewModel中的OrderDetails集合中,如下所示:
pricelistProductVM.Order.OrderDetails.Add(autoFillOd);
当我执行db.SaveChanges()时,我收到错误。
您会注意到错误发生在OrderDetails的子级上:OrderDetail_OrderDetailPricelistProductOptions_Source
为什么我不能动态地将OrderDetail添加到OrderDetails的集合中?所有OrderDetails都是新的(要插入),因此副本之间的值是相同的,但Quantity属性除外,这应该不是问题。
控制器操作如下:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Add(pricelistProductVM pricelistProductVM)
{
OrderLogic ol = new OrderLogic();
//Auth is running on execute
int userId = WebSecurity.CurrentUserId;
int websiteId = (int)Session["websiteId"];
int? id = null; // mediaId
int productId = pricelistProductVM.Product.ProductId;
int? eventId = pricelistProductVM.eventId;
string err = "";
if (productId > 0)
{
//Set Pricelist
Pricelist pricelist = ol.setPricelist(websiteId, id, eventId);
if (pricelist.PricelistId != 0)
{
//get the pricelistproduct from the pricelist
PricelistProduct pp = await (from ppx in db.PricelistProducts
where ppx.ProductId == productId
&& ppx.PricelistId == pricelist.PricelistId
&& ppx.isAvailable == true
&& ppx.DiscontinuedDate == null
&& ppx.Product.isAvailable == true
&& ppx.Product.DiscontinuedDate == null
select ppx).SingleOrDefaultAsync();
if (pp != null)
{
Order order = new Order();
//set some default values for the Order entity
if (pricelistProductVM.Order.OrderId == 0)
{
pricelistProductVM.Order.WebsiteId = websiteId;
pricelistProductVM.Order.UserId = userId;
pricelistProductVM.Order.EventId = eventId;
pricelistProductVM.Order.StartedDate = DateTime.UtcNow;
order = pricelistProductVM.Order;
}
else
{
order = await db.Orders.FindAsync(pricelistProductVM.Order.OrderId);
}
//set some default values for the OrderDetails entity
pricelistProductVM.Order.OrderDetails.First().InjectFrom(pp);
pricelistProductVM.Order.OrderDetails.First().IsPackage = false;
//determine if this product should be automatically added to any packages in the order
OrderDetail autoFillOd = ol.packageCheck(ref pp, ref pricelistProductVM, ref order, websiteId, db);
if (autoFillOd != null)
{
if (autoFillOd.Quantity > 0)
{
//This is where the OrderDetail that causes a problem is added
pricelistProductVM.Order.OrderDetails.Add(autoFillOd);
}
}
if (pricelistProductVM.Order.OrderId == 0)
{
db.Orders.Add(order);
}
else
{
order.OrderDetails.AddRange(pricelistProductVM.Order.OrderDetails);
db.Entry(order).State = EntityState.Modified;
}
db.SaveChanges();
}
else
{
//return error
err = "The product was not found in the available pricelist. Please reload your browser and make sure you are signed-in.";
}
}
}
else
{
//return error
err = "A productId was not passed so no product could not be found. Please reload your browser and make sure you are signed-in.";
}
if (err == "")
{
ViewBag.data = JsonConvert.SerializeObject(new { Success = 1, Msg = "The product was successfully added to your cart." });
}
else
{
ViewBag.data = JsonConvert.SerializeObject(new { Success = 0, Msg = err });
}
return View();
}
我很感激帮助!
答案 0 :(得分:0)
我认为OrderDetailPricelistProductOption.OrderDetailId不能单身 - &gt;它应该是一个列表,因为它可以出现在许多OrderDetails中......