Entityframework 6: Doubt to Persist a Relationship N to N

Asked

Viewed 209 times

0

Model:

public class Agenda
{
    public int Id { set; get; }
    public string Horario { set; get; }
    public string Local { set; get; }
    public virtual IEnumerable<Exame> Exames { set; get; }
}

public class Exame
{
    public int Id { set; get; }
    public string Descricao { set; get; }
    public virtual IEnumerable<Agenda> Agendas { set; get; }
}

Configurations of Entities:

public class AgendaMap : EntityTypeConfiguration<Agenda>
{
    public AgendaMap()
    {
        HasMany(x => x.Exames)
                .WithMany(x => x.Agendas)
                .Map(x => x.ToTable("AgendaExame"));
    }
}

public class ExameMap : EntityTypeConfiguration<Exame>
{
    public ExameMap()
    {
    }
}

Repository Method:

public void Inserir(T obj)
{
    banco.Set<T>().Add(obj);
    banco.SaveChanges();
}

I’m not able to find a solution to persist Agenda, because the bank generated the following tables (as I wanted):

Schedule (Id, Schedule, Location), Exam (Id, Description) and Scheduling(Agenda_id, Exame_id)

And with that, I must take, from the Schedule, the properties Id, Time and Location and save them in the Schedule table. Then I should take the Id of each Exam and also the Schedule Id and save in the Schedule Exam table. I don’t even know where I’m going...

Any tips? Thank you!

  • 1
  • Vishe, controversial rsrs. But why?

  • It is a commonplace, at least among Brazilian programmers, to accept repository repository as a good practice. In the answer I explain several reasons why it is not. If there is any more specific question, just ask.

1 answer

2


First, in your classes you should use Icollection in navigation properties. With Ienumerable you will not be able to modify.

In mapping:

public class AgendaMap : EntityTypeConfiguration<Agenda>
{
    public AgendaMap()
    {
        HasMany(x => x.Exames).WithMany(x => x.Agendas).Map(x => 
        {
            x.MapLeftKey("Agenda_Id");
            x.MapRightKey("Exame_Id");
            x.ToTable("AgendaExame"));
        }
    }
}

public class ExameMap : EntityTypeConfiguration<Exame>
{
    public ExameMap()
    {
        HasMany(x => x.Agendas).WithMany(x => x.Exames).Map(x => 
        {
            x.MapLeftKey("Exame_Id");
            x.MapRightKey("Agenda_Id");
            x.ToTable("AgendaExame"));
        }
    }
}

When creating or modifying do not need to worry about the Schedulescan table. It exists only for the application. You will only manipulate the collections.

That is, if you add an object like this:

var agenda = new Agenda()
{
    Horario = "12:00",
    Local = "São Paulo"
    new List<Exame>
    {
        Descricao = "alguma coisa"
    }
};

It will already popular the three tables.

Or to update existing objects:

var agendaJaExistente = this.SingleOrDefault(a => a.Id == id);
var novoExame = new Exame()
{
    Descricao = "alguma coisa",
    Agendas.Add(agendaJaExistente);
};

Browser other questions tagged

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