Entity Framework - N for N, clear list

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 to belong to no class.

I tried:

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, maintains 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"));
    
asked by anonymous 29.09.2014 / 14:29

1 answer

2

This will probably solve 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 more

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

I saw it in your question, you have a repository of objects and want to delete the user's classes from the student's repository.

The default repository, from what I understand, allows you more than one type of collection of objects because it caters to a business context (though that is not the issue yet).

You can also implement specific methods for each repository as needed.

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

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

Just add a method to remove classes:

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

Then:

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 needed 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 Clear() method, as you said in the comments.

    
29.09.2014 / 15:12