gpt4 book ai didi

lambda - 如何在 ASP.Net MVC 5 中保存父表的同时添加子表?

转载 作者:行者123 更新时间:2023-12-02 05:46:09 24 4
gpt4 key购买 nike

我正在使用 ASP.NET MVC 5 应用程序,我需要帮助在编辑父表时添加子表。即我需要在单个 Controller 方法中插入子表并更新父表。

我的用例是在创建和编辑 LabOrder(父表)时,将一组新的 ConsumerInventories(子表)添加到表中。在创建方法中,它工作正常,即添加新的消耗库存集,但在编辑方法中,它会导致错误。

我的模型://父表

public class LabOrder
{
[Key]
public int LabOrderID { get; set; }

[Required]
public string OrderNo { get; set; }

[Required(ErrorMessage="The Order Date field is required."), DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime OrderDate { get; set; }

[Required(ErrorMessage = "The Order Time field is required."), DataType(DataType.Time)]
public DateTime? OrderTime { get; set; }

[Required(ErrorMessage="The Hospital/ Clinic field is required.")]
public int EntityID { get; set; }

[Required]
public int LabOrderStatusID { get; set; }

[Required, Display(Name = "Patient")]
public int PatientID { get; set; }
}

//子表

public class ConsumedInventory
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ConsumedInventoryID { get; set; }

public int LabOrderID { get; set; }

public int InventoryItemID { get; set; }

public int ConsumedQuantity { get; set; }

public int EntityID { get; set; }
}

我的 Controller 代码:

public ActionResult EditLabOrder(LabOrder lab)
{
if (ModelState.IsValid)
{
ConsumedInventory AddConsumedInventory = new ConsumedInventory();
foreach (var stockItem in lab.ConsumedInventories.ToList())
{
AddConsumedInventory.ConsumedQuantity = stockItem.ConsumedQuantity;
AddConsumedInventory.EntityID = stockItem.EntityID;
AddConsumedInventory.InventoryItemID = stockItem.InventoryItemID;
AddConsumedInventory.LabOrderID = stockItem.LabOrderID;
db.ConsumedInventories.Add(AddConsumedInventory);
db.SaveChanges();
}
db.Entry(lab).State = EntityState.Modified;
db.SaveChanges();
}
}

我需要根据用户输入在子表中添加一到五个项目。

我就是这样的

Attaching an entity of type 'ConsumedInventory' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

我错过了什么?

最佳答案

我找到了解决方案..

不用使用 foreach 逐个添加记录.. 只需使用 EntityFramework 6 的名为 AddRange 的功能来一次添加记录...

这个对我来说效果很好

我已经更新了我的 Controller 代码:

public ActionResult EditLabOrder(LabOrder lab){
if (ModelState.IsValid)
{
db.ConsumedInventories.AddRange(lab.ConsumedInventories);
db.Entry(lab).State = EntityState.Modified;
db.SaveChanges();
}
}

关于lambda - 如何在 ASP.Net MVC 5 中保存父表的同时添加子表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39836135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com