Fix updating of entities

This commit is contained in:
Pat Hartl 2023-01-14 15:10:41 -06:00
parent 6156c87135
commit c525a5f38e
2 changed files with 47 additions and 3 deletions

View file

@ -311,13 +311,16 @@ namespace LANCommander.Controllers
if (ModelState.IsValid)
{
var game = await GameService.Get(viewModel.Game.Id);
var game = GameService.Get(g => g.Id == viewModel.Game.Id).FirstOrDefault();
game.Title = viewModel.Game.Title;
game.Description = viewModel.Game.Description;
game.ReleasedOn = viewModel.Game.ReleasedOn;
#region Update Developers
if (viewModel.SelectedDevelopers == null)
viewModel.SelectedDevelopers = new string[0];
foreach (var developer in game.Developers)
{
if (!viewModel.SelectedDevelopers.Any(d => d == developer.Name))
@ -334,6 +337,9 @@ namespace LANCommander.Controllers
#endregion
#region Update Publishers
if (viewModel.SelectedPublishers == null)
viewModel.SelectedPublishers = new string[0];
foreach (var publisher in game.Publishers)
{
if (!viewModel.SelectedPublishers.Any(p => p == publisher.Name))
@ -350,6 +356,9 @@ namespace LANCommander.Controllers
#endregion
#region Update Genres
if (viewModel.SelectedGenres == null)
viewModel.SelectedGenres = new string[0];
foreach (var genre in game.Genres)
{
if (!viewModel.SelectedGenres.Any(g => g == genre.Name))
@ -366,6 +375,9 @@ namespace LANCommander.Controllers
#endregion
#region Update Tags
if (viewModel.SelectedTags == null)
viewModel.SelectedTags = new string[0];
foreach (var tag in game.Tags)
{
if (!viewModel.SelectedTags.Any(t => t == tag.Name))
@ -381,6 +393,36 @@ namespace LANCommander.Controllers
}
#endregion
#region Update Actions
if (game.Actions != null)
{
game.Actions.Clear();
if (viewModel.Game.Actions != null)
{
foreach (var action in viewModel.Game.Actions)
{
game.Actions.Add(action);
}
}
}
#endregion
#region Update MultiplayerModes
if (game.MultiplayerModes != null)
{
game.MultiplayerModes.Clear();
if (viewModel.Game.MultiplayerModes != null)
{
foreach (var multiplayerMode in viewModel.Game.MultiplayerModes)
{
game.MultiplayerModes.Add(multiplayerMode);
}
}
}
#endregion
await GameService.Update(game);
return RedirectToAction(nameof(Index));

View file

@ -82,9 +82,11 @@ namespace LANCommander.Services
{
using (var repo = new Repository<T>(Context, HttpContext))
{
Context.Attach(entity);
var existing = await repo.Find(entity.Id);
entity = repo.Update(entity);
Context.Entry(existing).CurrentValues.SetValues(entity);
entity = repo.Update(existing);
await repo.SaveChanges();
return entity;