1

I'm trying to update a table(FlightClasses) with multiple rows having relation of one to many with table(Flight) but i can't update it with the changed values Can anyone help me with that

Model class of Flight i want to update rows on other table of flightclasses linked with row in this table

namespace Airline.Models
{
    public class FlightModel
    {
        public int Id { get; set; }
        public string FlightName { get; set; }
        public string FlightNo { get; set; }
        public string OriginCity { get; set; }
        public string DestinationCity { get; set; }
        public DateTime Departure { get; set; }
        public DateTime Arrival { get; set; }
        public virtual ICollection<FlightClassesModel> FlightClasses { get; set; }
    }
}

Model class of FlightClasses

namespace Airline.Models
{
    public class FlightClassesModel
    {

        public int FlightClassesModelId { get; set; }
        public type Class { get; set; }
        public int AvailableSeats { get; set; }
        public double Price { get; set; }
        public virtual FlightModel Flight { get; set; }

        public int? FlightId { get; set; }
    }
    public enum type
    {
        Business_Class,
        First_Class,
        Club_Class
    }
}

View

@model Airline.Models.FlightModel

@{
    ViewData["Title"] = "Edit";
}

<h2>Edit</h2>

<h4>FlightModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="Id" />
            <div class="form-group">
                <label asp-for="FlightName" class="control-label"></label>
                <input asp-for="FlightName" class="form-control" />
                <span asp-validation-for="FlightName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FlightNo" class="control-label"></label>
                <input asp-for="FlightNo" class="form-control" />
                <span asp-validation-for="FlightNo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="OriginCity" class="control-label"></label>
                <input asp-for="OriginCity" class="form-control" />
                <span asp-validation-for="OriginCity" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DestinationCity" class="control-label"></label>
                <input asp-for="DestinationCity" class="form-control" />
                <span asp-validation-for="DestinationCity" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Departure" class="control-label"></label>
                <input asp-for="Departure" class="form-control" />
                <span asp-validation-for="Departure" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Arrival" class="control-label"></label>
                <input asp-for="Arrival" class="form-control" />
                <span asp-validation-for="Arrival" class="text-danger"></span>
            </div>
            @foreach (var flightClass in Model.FlightClasses)
            {
                <div class="form-group">
                    <label asp-for="@flightClass.Class" class="control-label"></label>
                    <select class="form-control" asp-for="@flightClass.Class"  asp-items="Html.GetEnumSelectList<type>()"></select>
                    <span asp-validation-for="@flightClass.Class" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="@flightClass.AvailableSeats" class="control-label"></label>
                    <input asp-for="@flightClass.AvailableSeats" class="form-control" />
                    <span asp-validation-for="@flightClass.AvailableSeats" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="@flightClass.Price" class="control-label"></label>
                    <input asp-for="@flightClass.Price" class="form-control" />
                    <span asp-validation-for="@flightClass.Price" class="text-danger"></span>
                </div>
            }

            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

controller

public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var flightModel = await _context.Flight.FindAsync(id);
            if (flightModel == null)
            {
                return NotFound();
            }
            return View(flightModel);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, FlightModel flightModel)
        {
            if (id != flightModel.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(flightModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FlightModelExists(flightModel.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(flightModel);
        }
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236

2 Answers2

2

This code _context.Update(flightModel); will update flightModel only, so if you wanna update FlightClasses also you must get FlightClasses from the flightModel then use _context.Update(flightClasse); for all the instances in FlightClasses. And at the end you use await _context.SaveChangesAsync();

ilyes
  • 382
  • 2
  • 10
0

Updating data in the Parent-Child(one to many relation) table using EntityFramework Core

we first retrieve the FlightModel entity from the database using FirstOrDefaultAsync

   var entity = await _context.FlightModel
                .Include(s => s.FlightClassesModel)
                .FirstOrDefaultAsync(s => s.FlightModelId == id); 

use SetValues on the CurrentValues of the Entry method to update the properties of the FlightModel

 _context.Entry(entity).CurrentValues.SetValues(FlightModel);

Loop through the FlightClassesModel collection to update or add new child entities as needed.

foreach (var child in FlightModel.FlightClassesModel)
            {
                var childEntity = entity.FlightClassesModel.FirstOrDefault(c => c.FlightClassesModelId == child.FlightClassesModelId);
                if (childEntity != null)
                {
                    _context.Entry(childEntity).CurrentValues.SetValues(child);
                }
                else
                {
                    entity.FlightClassesModel.Add(child);
                }
            }

SaveChangesAsync to persist the changes to the database.

 await _context.SaveChangesAsync();
Xcode
  • 21
  • 3