C# 如何使用父id更新现有子类
目前我有一些想法,我们从父Id获取子数据,并用硬编码文本更新子数据。父类: ` 这种情况是,当我需要将“所有子状态”列更新为“确认”时,我需要首先通过搜索rideId找到它的父类,如果找到了骑乘,它将更新其子类属性。Im使用EF core保存数据C# 如何使用父id更新现有子类,c#,sql,asp.net,linq,ef-core-2.0,C#,Sql,Asp.net,Linq,Ef Core 2.0,目前我有一些想法,我们从父Id获取子数据,并用硬编码文本更新子数据。父类: ` 这种情况是,当我需要将“所有子状态”列更新为“确认”时,我需要首先通过搜索rideId找到它的父类,如果找到了骑乘,它将更新其子类属性。Im使用EF core保存数据 // PUT api/<controller>/5 [HttpPut("{id}/confirm")] public IActionResult ConfirmRide(int id, [FromBody]Ride ri
// PUT api/<controller>/5
[HttpPut("{id}/confirm")]
public IActionResult ConfirmRide(int id, [FromBody]Ride ride)
{
try
{
if (ride.IsObjectNull())
{
_logger.LogError("Ride object sent from client is null.");
return BadRequest("Ride object is null");
}
if (!ModelState.IsValid)
{
_logger.LogError("Invalid ride object sent from client.");
return BadRequest("Invalid model object");
}
var dbRide = _repository.Ride.GetRideById(id);
if (dbRide == null)
{
_logger.LogError($"Ride with id: {id}, hasn't been found in db.");
return NotFound();
}
_repository.Ride.ConfirmRide(dbRide, ride, id, "Confirmed");
//_repository.Ride.
_repository.Save();
return NoContent();
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside UpdateRide action: {ex.Message}");
return StatusCode(500, "Internal server error");
}
}
//PUT api//5
[HttpPut(“{id}/confirm”)]
公共IActionResult确认骑乘(int id,[FromBody]骑乘)
{
尝试
{
if(ride.IsObjectNull())
{
_LogError(“从客户端发送的骑乘对象为null”);
返回请求(“骑乘对象为空”);
}
如果(!ModelState.IsValid)
{
_logger.LogError(“从客户端发送的骑乘对象无效”);
返回BadRequest(“无效的模型对象”);
}
var dbRide=_repository.Ride.GetRideById(id);
if(dbRide==null)
{
_logger.LogError($“id为:{id}的骑行,在db中未找到。”);
返回NotFound();
}
_repository.Ride.confirmrride(dbRide,Ride,id,“已确认”);
//_骑自行车。
_Save();
返回NoContent();
}
捕获(例外情况除外)
{
_logger.LogError($“UpdateRide操作中出错:{ex.Message}”);
返回状态码(500,“内部服务器错误”);
}
}
目前这是我保存或更新数据的逻辑,你们能帮助我如何根据父Id更新子类库吗。
我得到了这个解决方案,并用其他资源对其进行了修改
public void ConfirmRide(Ride dbRide, int id, string status)
{
dbRide.MapStatus(status);
Update(dbRide);
var existingParent = RepositoryContext.Rides
.Where(p => p.Id == id)
.Include(p => p.Requests).Where(r => r.Requests.Any( request => request.Status == "Approved"))
.SingleOrDefault();
if (existingParent != null)
{
foreach (var existingChild in existingParent.Requests.ToList())
{
existingChild.Status = "Confirmed";
}
}
RepositoryContext.SaveChanges();
}
那么,
Ride.confirmrride
中会发生什么呢?我没有看到任何试图更新“子状态列”的代码——甚至根本没有状态列。是的,我在这一点上卡住了,你能帮我访问和更新子值吗。
// PUT api/<controller>/5
[HttpPut("{id}/confirm")]
public IActionResult ConfirmRide(int id, [FromBody]Ride ride)
{
try
{
if (ride.IsObjectNull())
{
_logger.LogError("Ride object sent from client is null.");
return BadRequest("Ride object is null");
}
if (!ModelState.IsValid)
{
_logger.LogError("Invalid ride object sent from client.");
return BadRequest("Invalid model object");
}
var dbRide = _repository.Ride.GetRideById(id);
if (dbRide == null)
{
_logger.LogError($"Ride with id: {id}, hasn't been found in db.");
return NotFound();
}
_repository.Ride.ConfirmRide(dbRide, ride, id, "Confirmed");
//_repository.Ride.
_repository.Save();
return NoContent();
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside UpdateRide action: {ex.Message}");
return StatusCode(500, "Internal server error");
}
}
public void ConfirmRide(Ride dbRide, int id, string status)
{
dbRide.MapStatus(status);
Update(dbRide);
var existingParent = RepositoryContext.Rides
.Where(p => p.Id == id)
.Include(p => p.Requests).Where(r => r.Requests.Any( request => request.Status == "Approved"))
.SingleOrDefault();
if (existingParent != null)
{
foreach (var existingChild in existingParent.Requests.ToList())
{
existingChild.Status = "Confirmed";
}
}
RepositoryContext.SaveChanges();
}