Как обновить сложную модель?C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Гость
 Как обновить сложную модель?

Сообщение Гость »


У меня есть модель зала, представляющая зал, и если зал пуст, у меня есть функция, которая создает событие и связывает с ним места, ряды и области в соответствии с этим залом.
Это означает создание HallEvent с залом, HallEventArea с использованием области, событие HallEventrow со строкой и HallEventSeats с местом.
Проблема в том, что когда я обновляю зал (например, добавляя места, добавляя ряды или удаляя области и т. д. )
Мне нужно обновить само событие целиком (также HallEventArea, HallEventRow и HallEventSeat)
Дело в том, что в Create HallEvent я начал «сверху вниз» (от HallEvents до HallEventsSeat)
А сейчас я не могу этого сделать, потому что между моделями нет прямой связи.
Прикрепляю изображение всех связанных таблиц
Изображение
В конце концов мне нужны ряды, места и области события должны быть обновлены в соответствии с HallSource (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 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);
}
}

}
}


Источник: https://stackoverflow.com/questions/781 ... plex-model
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»