Home > OS >  How to add entity inside another entity
How to add entity inside another entity

Time:09-16

I am working on .Net Core

This is my mission page. Its releated with two table actually.enter image description here

In the old tradition you have to fill each table before you pick that data from here

but what i want is to add new drivers from here.

enter image description here

To be clear, I can pick two drivers Vin and Jason however if i text here something else and if i press the add mission button It should be added to Drivers table and Missions Table too

I tried:

[HttpPost]
public async Task<ActionResult<List<Mission>>> AddMission(MissionDto request)
{
    var driver = _context.Drivers
    .include(p=>p.Missions)
    .SingleOrDefault(p => p.Name == request.name)
    ?? new Driver
    {
        Name = request.Name,
        Age = request.Age
        //...
        //...
    };

    var newMission = new Mission
    {
        TimeCreated = Convert.ToDateTime(DateTime.Now.GetDateTimeFormats()[0]),
        MissionId = request.ShipmentId,
    };

    driver.Missions.Add(newMission);
    _context.SaveChanges();
}

I have concerns about new drivers to be added. Is it correct to use this way? Thanks in advance for your advice and help.

CodePudding user response:

You have to add new Driver explicitly and Include Missions collection:

[HttpPost]
public async Task<ActionResult<List<Mission>>> AddMission(MissionDto request)
{
    var driver = _context.Drivers
        .Include(d => d.Missions)
        .SingleOrDefault(p => p.Name == request.name);

    if (driver == null)
    {
        driver = new Driver
        {
            Name = request.Name,
            Age = request.Age
            //...
            //...
        };

        _context.Drivers.Add(driver);
    }

    var newMission = new Mission
    {
        TimeCreated = Convert.ToDateTime(DateTime.Now.GetDateTimeFormats()[0]),
        MissionId = request.ShipmentId,
    };

    driver.Missions.Add(newMission);

    _context.SaveChanges();
}

CodePudding user response:

You can do this:

[HttpPost]
public async Task<ActionResult<List<Mission>>> AddMission(MissionDto request, CancellationToken cancellationToken)
{
    var driver = new Driver();

    driver = await _context.Drivers
        .Include(driver => driver.Missions)
        .Where(driver => driver.Name == request.Name)
        .SingleOrDefaultAsync(cancellationToken);

    var mission = new Mission
    {
        TimeCreated = Convert.ToDateTime(DateTime.Now.GetDateTimeFormats()[0]),
        MissionId = request.ShipmentId,
    };

    if(driver is not null)
    {
         driver.Missions.Add(mission);
        _context.driver.Update(driver)
    }

    if (driver is null)
    {
        driver = new Driver
        {
            Name = request.Name,
            Age = request.Age
            //...
            //...
        };

        driver.Missions.Add(mission);
        await _context.Drivers.AddAsync(driver,cancellationToken);
    }

   
   await _context.SaveChangesAsync(cancellationToken);
}
  • Related