【发布时间】:2020-03-21 04:44:28
【问题描述】:
目前我有一些想法,我们从其父 ID 获取子数据,并使用硬编码文本更新子数据。家长班: `
public class Ride
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime RideStartTime { get; set; }
public string DestinationLongitude { get; set; }
public string DestinationLatitude { get; set; }
public int SeatAvailable { get; set; }
public Double TotalCost { get; set; } = 0;
public Double TotalDistance { get; set; } = 0;
//Ride has Many Request
public ICollection<Request> Requests { get; set; }
}
`
儿童班
public class Request : IEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string PickupLongitude { get; set; }
[Required]
public string PickupLatitude { get; set; }
public Double? EstimatedCost { get; set; } = 0;
public Double? Rating { get; set; } = 0;
public int RideId { get; set; }
public Ride Ride { get; set; }
}
情况是当我需要将所有子状态列更新为“确认”时,我需要首先通过搜索rideId 找到它的父类,如果找到了乘车,它将更新他们的子类属性。我使用 EF 核心来保存数据。
// 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");
}
}
目前这是我保存或更新数据的逻辑,你们能帮我如何根据父 ID 更新子类吗?
【问题讨论】:
-
那么
Ride.ConfirmRide会发生什么?我没有看到任何尝试更新“子状态列”的代码——甚至根本没有状态列。 -
是的,我卡在那一点上,你能帮我访问和更新孩子的价值吗?
标签: c# sql asp.net linq ef-core-2.0