I am working on a scenario where I have the following entities with a many to many relationship:
public class Tutor
{
public int TutorId { get; set; }
public string Name { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
}
public class TutorStudent
{
public int TutorId { get; set; }
public Tutor Tutor { get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
}
and the following Dtos
public class StudentDto
{
public int StudentId { get; set; }
public string Name { get; set; }
public IList<TutorDto> Tutors { get; set; }
}
public class StudentInputDto
{
public int StudentId { get; set; }
public string Name { get; set; }
public IList<int> SelectedTutors { get; set; }
}
public class TutorDto
{
public int TutorId { get; set; }
public string Name { get; set; }
}
I have two methods:
[HttpGet]
public async Task<StudentDto> UpdateStudent(EntityDto<int> input)
{
return student from database
}
[HttpPost]
public async Task UpdateStudent(StudentInputDto input)
{
if (input == null)
throw new NoDataSentException();
Student existingStudent = await (from student in studentRepository.GetAll()
.Include(x => x.Tutor)
where student.Id == input.Id
select student).SingleOrDefaultAsync();
ObjectMapper.Map(input, existingStudent);
await studentRepository.UpdateAsync(existingStudent);
}
The get method works but for the post how do I do the mapping to the many-to-many table?
4 Answer(s)
-
0
Can you please change your ~~table~~ tables as seen that link below. Then try if you have received the same error. https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration
-
0
@demirmusa Which table?
-
0
Hi @antonis
You can try to configure the relation like this https://docs.microsoft.com/en-us/ef/core/modeling/relationships#many-to-many but I'm not sure if EF Core will delete the related entities or not. I think it is not supported by EF core but couldn't find any document about it.
-
0
I have to remove them manually.