Entity Framework - N to N, clear list

Asked

Viewed 151 times

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.

1 answer

2


Probably that solves your problem:

var bdAluno = new AlunoRepositorioEF(contexto);
var aluno = bdAluno.Get(x => x.AlunoID == ID).FirstOrDefault();

aluno.Turmas.Clear();
bdAluno.Atualizar(aluno);
bdAluno.SalvarTodos();

Extending the subject a little further

For both cases, you need to load the Classes. I believe you already do this directly or through Lazy Loading.

By the vi in your question, you have an object repository and want to delete the user classes by the student repository.

The Repository pattern, from what I understand, allows you to more than one kind of object collection because it meets a business context (although that’s not the issue yet).

You can also implement specific methods for each repository, according to the need.

As an example, and based on your repository pattern, I would say you have something like this:

public class AlunoRepositorioEF : Repositorio<AppContext, Aluno>
{
    public AlunoRepositorioEF(AppContext context) { 
        _context = context;
    }
    ...
}

Simply add a method to remove classes:

public void RemoverTurmas(int id)
{
    var aluno = Get(x => x.AlunoID == ID).FirstOrDefault();
    aluno.Turmas.Clear();
}

So:

var bdAluno = new AlunoRepositorioEF(contexto);
dbAluno.RemoverTurmas(id);

Also, in each repository you can have an interface of the same to add more methods that are necessary for that business context.

For example:

public interface IAlunoRepositorioEF 
{
    void RemoverTurmas(int id);
}

public class AlunoRepositorioEF : IAlunoRepositorioEF, Repositorio<AppContext, Aluno>
{
    public AlunoRepositorioEF(AppContext context) { 
        _context = context;
    }
    ...

    public void RemoverTurmas(int id)
    {
        var aluno = Get(x => x.AlunoID == ID).FirstOrDefault();
        aluno.Turmas.Clear();
    }
}

Editing

I switched to the method Clear(), as you said in the comments.

  • My student. Classes don’t have the Removeall method, they only have Remove. (Which worked to delete a class)

  • I found the Clear method, works the same.

  • Another question is: is it really necessary to create an interface to be implementing this method? I couldn’t just add to Alunorepositorioef without implementing an interface?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.