I have a form on a react-based web game with a form that contains both dungeon and hero data.
After the player fills it out and hits submit, it is sent to my .NetCore 3.1 Entity Framerwork API.
The API needs to save it to 2 different models so that it can be inserted into 2 different database tables.
Those are Dungeon and Heroes.
I have a controller that is supposed to accept the form data and it currently looks like what you see below.
I've figured out how to use one model to update one database table, but how do I do it for two?
Here is my non-working controller code that I've started to write out:
[HttpPost]
public async Task<ActionResult<CreateDungeon>> ProcessForm([FromBody]CreateDungeon data)
{
// Read quest data from form and add to DB via EF
_context.Quest.Add(data.Quest);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (QuestExists(data.Quest.Id))
{
return Conflict();
}
else
{
throw;
}
}
// Read heroes data from form and add to DB via EF
_context.Heroes.Add(data.Heroes);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (HeroesExists(data.Heroes.Id))
{
return Conflict();
}
else
{
throw;
}
}
return Ok();
}
Is this the correct way of doing this?
Thanks!