這兩個(gè)實(shí)體是一對(duì)多關(guān)系(由代碼優(yōu)先f(wàn)luent api構(gòu)建)。public class Parent{ public Parent() { this.Children = new List<Child>(); } public int Id { get; set; } public virtual ICollection<Child> Children { get; set; }}public class Child{ public int Id { get; set; } public int ParentId { get; set; } public string Data { get; set; }}在我的WebApi控制器中,我可以執(zhí)行一些操作來(lái)創(chuàng)建一個(gè)父實(shí)體(工作正常)并更新一個(gè)父實(shí)體(存在一些問(wèn)題)。更新操作如下所示:public void Update(UpdateParentModel model){ //what should be done here?}目前我有兩個(gè)想法:獲得命名為跟蹤父實(shí)體existing通過(guò)model.Id,并在指定值model逐一的實(shí)體。這聽(tīng)起來(lái)很愚蠢。而且model.Children我不知道哪個(gè)孩子是新孩子,哪個(gè)孩子被修改(甚至刪除)。通過(guò)創(chuàng)建一個(gè)新的父實(shí)體model,并將其附加到DbContext并保存。但是DbContext如何知道子狀態(tài)(新的添加/刪除/修改)?實(shí)施此功能的正確方法是什么?
- 3 回答
- 0 關(guān)注
- 942 瀏覽
添加回答
舉報(bào)
0/150
提交
取消