实体框架 6 的多重性为 1 或 0.1
本文关键字:多重性 框架 实体 | 更新日期: 2023-09-27 18:34:23
模型绑定器创建模型、子项和孙项并使用上下文后,尝试将对象插入子集合时,我收到以下错误。保存更改((;
违反了多重性约束。关系"PPLib.Models.OrderDetail_OrderDetailPricelistProductOptions"的角色"OrderDetail_OrderDetailPricelistProductOptions_Source"具有多重性 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; }
}
更清楚的是:
如果我提交一个完整的新订单,其中包含订单详细信息列表、订单详细信息价格列表产品选项列表和订单详细信息价格清单产品选项详细信息列表,则模型活页夹将完成其工作,并且我不会收到以下错误:
db.Orders.Add(order);
db.SaveChanges();
如果我提交"编辑"和"现有订单",并提交"新建订单详细信息"列表、"订单详细信息价格列表产品选项"列表和"订单详细信息价格列表产品选项详细信息"列表,则我从数据库上下文中获取订单,然后使用以下命令合并视图模型中的订单详细信息:
order.OrderDetails.AddRange(pricelistProductVM.Order.OrderDetails);
并且我在执行以下操作时没有收到任何错误:
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
我有一个特殊的情况,我必须实例化一个名为autoFillOd的新OrderDetail,并从模型绑定器组装的现有OrderDetails之一注入其值。我更改了它的数量值,然后将其添加到 ViewModel 中的订单详细信息集合中,如下所示:
pricelistProductVM.Order.OrderDetails.Add(autoFillOd);
当我做数据库时。保存更改((,我收到错误。
您会注意到错误出在订单详细信息的子项上:OrderDetail_OrderDetailPricelistProductOptions_Source
为什么我不能将订单详细信息动态添加到订单详细信息集合中?所有订单详细信息都是新的(要插入(,因此副本之间的值相同,除了 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();
}
我感谢您的帮助!
我认为OrderDetailPricelistProductOption.OrderDetailId不能是单一的 ->它应该是一个列表,因为它可以出现在许多OrderDetails中...