Hi all,
I'm trying to define a Web API to insert data in a many-to-many tables structure. I'm using EF Core code first approch.
This is my model data:
public class Giocata
{
public int GiocataId { get; set; }
public string NomeGiocata { get; set; }
// FK GiocateRuote
public ICollection<GiocataRuota> GiocateRuote { get; set; }
}
public class Ruota
{
public int RuotaId { get; set; }
public string NomeRuota { get; set; }
// FK GiocateRuote
public ICollection<GiocataRuota> GiocateRuote { get; set; }
}
public class GiocataRuota
{
public int GiocataRuotaId { get; set; }
// FK RuotaId
public int RuotaId { get; set; }
public Ruota Ruota { get; set; }
// FK GiocataId
public int GiocataId { get; set; }
public Giocata Giocata { get; set; }
}
In order to pass datas to my web api I'm using the dto, so I have also defined:
public class GiocataDTO
{
public Giocata Giocata { get; set; }
public List<Ruota> lstRuota { get; set; }
public List<Sortita> lstSortita { get; set; }
public List<Importo> lstImporto { get; set; }
}
So, in my controller I have defined the method as follow:
[HttpPost]
[Route("[action]")]
[Route("api/giocate/PostSalvaGiocata")]
public async Task<ActionResult<GiocataDTO>> PostSalvaGiocata([FromBody] GiocataDTO giocatadto)
{
var lstGiocateRuota = new List<GiocataRuota>();
foreach (var ruota in giocatadto.lstRuota)
{
var giocataruotain = new GiocataRuota { Ruota = ruota, Giocata = giocatadto.Giocata };
lstGiocateRuota.Add(giocataruotain);
}
await _context.AddRangeAsync(giocatadto.Giocata, giocatadto.lstRuota, lstGiocateRuota);
await _context.SaveChangesAsync();
return CreatedAtAction("postSalvaGiocata", new { id = giocatadto.Giocata.GiocataId }, giocatadto.Giocata);
}
Now when I try to consume the Api I receive this error:
System.InvalidOperationException: The entity type 'List<Ruota>' was not found. Ensure that the entity type has been added to the model.
This is my forst time I use a many-to-many procedure so I'm not able to understand where the problem is.
Could someone halp me please to understand what can I check?
Thank you for any kind of help.
Igor