如何更新关联Hall的HallEvent复杂模型?
问题描述
我有一个表示大厅的Hall模型,当大厅为空时,会通过函数创建一个HallEvent,并关联该大厅对应的区域、排和座位——即创建关联Hall的HallEvent、关联Area的HallEventArea、关联Row的HallEventRow以及关联Seat的HallEventSeat。
当更新Hall(例如添加座位、添加排或删除区域等操作)时,需要同步更新对应的HallEvent及其关联的HallEventArea、HallEventRow和HallEventSeat。此前创建HallEvent时采用自上而下的方式(从HallEvent到HallEventSeat),但现在由于模型间无直接关联,无法沿用该方式。
最终需求是让HallEvent的区域、排、座位与源Hall保持一致,以下是我目前的代码,但尚未找到正确的实现方法:
[HttpPost] [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] public ActionResult Index(Guid hallId, Guid eventId, RegistrationDetials registrationDetials) { if (hallId == null) return RedirectToAction("Index", "Halls"); if (CustomerId == 0) { return HttpNotFound(); } int customerId = CustomerId; //get hall event if exists var hallEventToUpdate = hallEventsRepo.GetByIdAndCustomerId(eventId, customerId); Hall hallSource = hallsRepo.GetByIdAndCustomerId(hallId, customerId); List<HallEventSeat> eventSeats = hallEventSeatsRepo.GetHallEventSeatsByCustomerId(eventId, customerId); //.Select(x => new SeatModel(x)) //.ToList(); // comment: if there are no seats for the event we need to create it // else, we need to update statuses for the seats that need to be free. if(eventSeats.Count != 0) { foreach(var hallEventAreaToUpdate in hallEventToUpdate.HallEventAreas) { var area = hallSource.Areas.SingleOrDefault(x=> x.Name == hallEventAreaToUpdate.Name); if (area == null) { continue; } hallEventAreaToUpdate.Angle = area.Angle; hallEventAreaToUpdate.AreaTypeId = area.AreaTypeId; hallEventAreaToUpdate.CreatDate = area.CreatDate; hallEventAreaToUpdate.Deleted = area.Deleted; hallEventAreaToUpdate.CreateUser = area.CreateUser; hallEventAreaToUpdate.ModifyDate = DateTime.Now; hallEventAreaToUpdate.Height= area.Height; hallEventAreaToUpdate.LastUser = area.LastUser; hallEventAreaToUpdate.Left = area.Left; hallEventAreasRepo.Update(hallEventAreaToUpdate); if(area.Rows !=null) { foreach(var row in area.Rows) { var hallEventRowToUpdate = hallEventAreaToUpdate.HallEventRows.SingleOrDefault(x=>x.Name== row.Name); if (hallEventRowToUpdate == null) { hallEventRowToUpdate = new HallEventRow() { Angle = row.Angle, Height = row.Height, Left = row.Left, Name = row.Name, Status = row.Status, Top = row.Top, Width = row.Width, CreatDate = row.CreatDate, CreateUser = row.CreateUser, ModifyDate = row.ModifyDate, LastUser = row.LastUser, Deleted = row.Deleted, HallEventAreaId = hallEventAreaToUpdate.Id, HallEventArea = hallEventAreaToUpdate, }; hallEventAreaToUpdate.HallEventRows.Add(hallEventRowToUpdate); } hallEventRowToUpdate.Angle = row.Angle; hallEventRowToUpdate.Height = row.Height; hallEventRowToUpdate.Left = row.Left; hallEventRowToUpdate.Name = row.Name; hallEventRowToUpdate.Status = row.Status; hallEventRowToUpdate.Top = row.Top; hallEventRowToUpdate.Width = row.Width; hallEventRowToUpdate.CreatDate = row.CreatDate; hallEventRowToUpdate.CreateUser = row.CreateUser; hallEventRowToUpdate.ModifyDate = row.ModifyDate; hallEventRowToUpdate.LastUser = row.LastUser; hallEventRowToUpdate.Deleted = row.Deleted; hallEventRowToUpdate.HallEventAreaId = hallEventAreaToUpdate.Id; hallEventRowToUpdate.HallEventArea = hallEventAreaToUpdate; hallEventAreasRepo.Update(hallEventAreaToUpdate); } var rowsToDelete = hallEventAreaToUpdate.HallEventRows .Where(x => !area.Rows.Any(y => y.Id == x.Id)); //need to delete rows; } if (area.Seats != null) { foreach(var seat in area.Seats) { var HallEventSeatToUpdate = hallEventAreaToUpdate.HallEventSeats.SingleOrDefault(x => x.Id == seat.Id); if (HallEventSeatToUpdate == null) { HallEventSeatToUpdate = new HallEventSeat() { Angle = seat.Angle, Left = seat.Left, Name = seat.Name, Height = seat.Height, Number = seat.Number, SeatId = seat.Id, Status = seat.Status, Top = seat.Top, Width = seat.Width, CreatDate = seat.CreatDate, CreateUser = seat.CreateUser, ModifyDate = seat.ModifyDate, LastUser = seat.LastUser, Deleted = seat.Deleted, //HallEventRowId = HallEventSeat, //HallEventRow = hallEventRows, HallEventAreaId = hallEventAreaToUpdate.Id, HallEventArea = hallEventAreaToUpdate, }; } var row = hallEventAreaToUpdate.HallEventRows .SingleOrDefault(x => (x.Id == 0 && x.HallEventArea.Id == seat.AreaId && x.Name == seat.Row.Name) || (x.Id == seat.RowId)); HallEventSeatToUpdate.HallEventRowId = row.Id; HallEventSeatToUpdate.HallEventRow = row; if(HallEventSeatToUpdate.Id == 0) { hallEventSeatsRepo.Create(HallEventSeatToUpdate); seat.Id = HallEventSeatToUpdate.Id; } else { hallEventSeatsRepo.Update(HallEventSeatToUpdate); } } var seatsToDelete = hallEventAreaToUpdate.HallEventSeats .Where(x => !area.Seats.Any(y => y.Id == x.Id)) .ToList(); foreach(var seatToDelete in seatsToDelete) { hallEventSeatsRepo.Delete(seatToDelete); } } } } }
内容的提问来源于stack exchange,提问作者Hai Elimelech
相关产品推荐
相关产品推荐

