【发布时间】:2019-08-14 22:56:29
【问题描述】:
我有一个 API,我希望能够在其中执行 PUT 请求,并在其中发布带有 Blocks 数组的 Plan。目前,它会自动添加新的 Block 条目并更新现有条目,但如果帖子中缺少条目,它不会删除它们。
这些是模型:
public class Plan
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Block> Blocks { get; set; }
}
public class Block
{
public int Id { get; set; }
public string Name { get; set; }
public int PlanId { get; set; }
public Plan Plan { get; set; }
}
控制器动作:
[HttpPut]
public ActionResult Update(Plan plan)
{
if (plan?.Id == null) return BadRequest();
var plans = _context.Plans.Count(p => p.Id == plan.Id);
if (plans != 1) return NotFound();
_context.Update(plan);
_context.SaveChanges();
return NoContent();
}
我将如何实现这一点? EF中是否有启用此功能的设置?
我正在寻找一个通用的解决方案,因为会有更多的集合和模型需要相同的功能。我不想为每个控制器/动作都编写这个程序。
我在 .net core 2.2 上使用 Entity Framework Core
【问题讨论】:
-
你能在更新
Plan实体的地方添加代码示例吗? -
@Fourat 当然,我添加了它
-
在类似的情况下,我必须创建一个 UpdateEntity 方法来获取数据库实体和请求实体。 here is a link to the more detailed answer
标签: .net-core entity-framework-core