3
public class Aluno
{
public int AlunoID { get; set; }
public virtual ICollection<Turma> Turmas { get; set; }
}
public class Turma
{
public int TurmaID { get; set; }
public virtual ICollection<Aluno> Alunos { get; set; }
}
A student already belongs to a series of classes, so I want the student not to belong to any class.
I tried to:
var bdAluno = new AlunoRepositorioEF(contexto);
var aluno = bdAluno.Get(x => x.AlunoID == ID).FirstOrDefault();
//aluno.Turmas = null; //Tentei este jeito também.
aluno.Turmas = new List<Turma>();
bdAluno.Atualizar(aluno);
bdAluno.SalvarTodos();
But he does nothing, keeps relationships.
public void Atualizar(TEntity obj)
{
ctx.Entry<TEntity>(obj).State = EntityState.Modified;
}
public void SalvarTodos()
{
ctx.SaveChanges();
}
Context:
modelBuilder.Entity<Aluno>()
.HasMany(t=>t.Turmas)
.WithMany(a => a.Alunos)
.Map(m => m.MapLeftKey("Aluno_AlunoID")
.MapRightKey("Turma_TurmaID")
.ToTable("Area_Cursos_TurmaAluno"));
A
foreach
in the student classes deleting the class may work.– Filipe Oliveira