EF不查询直接修改,修改部分字段,避开设置有[Required]字段的验证检查
程序员文章站
2024-02-20 15:03:58
...
[HttpGet]
public async Task<IHttpActionResult> Restoration(int id)
{
//只修改状态值Status
Institution model = new Institution();
model.Id = id;
model.Status = 5;
//model.Title = "测试99"; //这个字段设置了 [Required]检查
//model.SerialNo = "测试99"; //这个字段设置了 [Required]检查
DbEntityEntry<Institution> entry = dbContext.Entry<Institution>(model);
entry.State = System.Data.Entity.EntityState.Unchanged;
//手动设置只修改此字段
entry.Property("Status").IsModified = true;
//await dbContext.SaveChangesAsync(); //如果用此方法保存会验证设置了[Required]标签的字段必须非空
//避开设置了 [Required]字段的验证检查
await ((IObjectContextAdapter)dbContext)
.ObjectContext.SaveChangesAsync();
return Ok("执行成功");
}
转载:
https://www.cnblogs.com/keyindex/archive/2012/06/24/2559711.html