【问题标题】:How to update existing child class by using the parent id如何使用父 ID 更新现有的子类
【发布时间】: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


【解决方案1】:

How to add/update child entities when updating a parent entity in EF

我得到了这个解决方案并用其他资源对其进行了修改。

 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();


    }

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 2020-12-04
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多