Next I'm creating a webapi (I'm using the Entity Framework) with two classes, Course and Discipline, where the course has the Id of a course, I created it as follows:
public class Curso
{
public int Id { get; set; }
public string Nome { get; set; }
public List<Disciplina> Disciplinas{ get; set; }
}
public class Disciplina
{
public int Id { get; set; }
public string Nome { get; set; }
public int CursoId { get; set; }
public Curso Curso{ get; set; }
}
The OnModelCreating method looks like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Disciplina>()
.HasOne(p => p.Curso)
.WithMany(p => p.Disciplinas)
.HasForeignKey(p => p.CursoId);
base.OnModelCreating(modelBuilder);
}
In the database created certain, Course has Id and Name and Discipline has Id, Name and CourseId, however when I make the GET requests the result is the following:
[
{
"id": 1,
"nome": "SI",
"disciplinas": 1,
}
]
[
{
"id": 1,
"nome": "POO",
"cursoId": 1,
"curso": null
}
]
Is there any way for Entity to map better so that those keys that are coming NULL do not appear in JSON?
Remembering that I need only the course ID in the course.