C# 更新实体框架核心中的多对多关系
我正在尝试使用实体框架核心更新C# 更新实体框架核心中的多对多关系,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,我正在尝试使用实体框架核心更新ASP.NET核心MVC控制器中的多对多关系。我成功地将其添加到关系中,但没有更新(如果我只是打开/保存实体,则会导致重复的键错误) 在以有效的方式更新/插入新关系之前,如何从数据库中删除关系 public async Task<IActionResult> Edit(int id, [Bind("Id,Name,SalesClerkIds")] Plant plant) { if (id != plant.Id) {
ASP.NET核心MVC
控制器中的多对多
关系。我成功地将其添加到关系中,但没有更新(如果我只是打开/保存实体,则会导致重复的键错误)
在以有效的方式更新/插入新关系之前,如何从数据库中删除关系
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,SalesClerkIds")] Plant plant)
{
if (id != plant.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
plant.SalesClerks = new List<PlantSalesClerk>();
if (plant.SalesClerkIds != null)
{
foreach (var scId in plant.SalesClerkIds)
{
plant.SalesClerks.Add(new PlantSalesClerk()
{
Plant = plant,
User = _context.Users.FirstOrDefault(u => u.Id == scId)
});
}
}
_context.Update(plant);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PlantExists(plant.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(plant);
}
public async Task Edit(int-id,[Bind(“id,Name,SalesClerkIds”)]Plant工厂)
{
如果(id!=工厂id)
{
返回NotFound();
}
if(ModelState.IsValid)
{
尝试
{
plant.sales clerks=新列表();
if(plant.SalesClerkIds!=null)
{
foreach(植物中的var scId.SalesClerkIds)
{
plant.salesclers.Add(新plantsalesclers()
{
植物,
User=\u context.Users.FirstOrDefault(u=>u.Id==scId)
});
}
}
_更新(工厂);
wait_context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
如果(!plant存在(plant.Id))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回重定向到操作(名称(索引));
}
返回视图(工厂);
}
按如下方式编写您的编辑方法:
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,SalesClerkIds")] Plant plant)
{
if (id != plant.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
Plant plantToBeUpdated = await _context.Plants.Include(p => p.SalesClerks).FirstOrDefaultAsync(p => p.Id == id);
if (plantToBeUpdated != null)
{
plantToBeUpdated.SalesClerks.Clear(); // Here you have to clear the existing children before adding the new
if (plant.SalesClerkIds.Count > 0)
{
foreach (var scId in plant.SalesClerkIds)
{
plantToBeUpdated.SalesClerks.Add(new PlantSalesClerk()
{
PlantId = plantToBeUpdated.Id,
UserId = scId
});
}
}
plantToBeUpdated.Name = plant.Name;
// Map other properties here if any
_context.Plants.Update(plantToBeUpdated);
await _context.SaveChangesAsync();
}
}
catch (DbUpdateConcurrencyException)
{
if (!PlantExists(plant.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(plant);
}
public async Task Edit(int-id,[Bind(“id,Name,SalesClerkIds”)]Plant工厂)
{
如果(id!=工厂id)
{
返回NotFound();
}
if(ModelState.IsValid)
{
尝试
{
Plant plantToBeUpdated=wait _context.Plants.Include(p=>p.SalesClerks).FirstOrDefaultAsync(p=>p.Id==Id);
if(plantToBeUpdated!=null)
{
plantToBeUpdated.SalesClerks.Clear();//在这里,您必须在添加新的子项之前清除现有子项
如果(plant.SalesClerkIds.Count>0)
{
foreach(植物中的var scId.SalesClerkIds)
{
plantToBeUpdated.SalesClerks.Add(新的PlantSalesClerk()
{
PlantId=plantToBeUpdated.Id,
UserId=scId
});
}
}
plantToBeUpdated.Name=plant.Name;
//在此处映射其他属性(如果有)
_context.Plants.Update(plantToBeUpdated);
wait_context.SaveChangesAsync();
}
}
catch(DbUpdateConcurrencyException)
{
如果(!plant存在(plant.Id))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回重定向到操作(名称(索引));
}
返回视图(工厂);
}
注意:我没有看到您的模型类和编辑视图。我已经根据你的代码假设了一切。因此,可能需要进行一些调整,但这是在EF core中使用子项更新模型的概念。非常感谢。。。它是这样工作的!我原以为会有一种更“自动化”的方法来做这件事,但我对这个解决方案也很满意。所以,它只是“删除所有内容并将其添加为新内容”。。?