Mapping of Associative tables with Entity Framework

Asked

Viewed 466 times

2

I have the following problem: A client has tasks for each day of the week.

Example:

Client A - Saturday - Wash Car, Clean House...

Map it as follows:

public class Cliente
{
    [Key]
    public int Id { get; set; }

    [MaxLength(250)]
    public string Nome { get; set; }

    public virtual ICollection<DiaSemana> DiasSemana { get; set; }
}

public class DiaSemana
{
    public DiaSemana()
    {
        Tarefas = new List<Tarefa>();
        Clientes = new List<Cliente>();
    }

    [Key]
    public int Id { get; set; }

    [MaxLength(20)]
    public string Dia { get; set; }


    public virtual ICollection<Tarefa> Tarefas { get; set; }

    public virtual ICollection<Cliente> Clientes { get; set; }
}

public class Tarefa
{
    public Tarefa()
    {
        DiasSemana = new List<DiaSemana>();
    }

    [Key]
    public int Id { get; set; }

    [MaxLength(20)]
    public string Nome { get; set; }

    public virtual ICollection<DiaSemana> DiasSemana { get; set; }
}

When I first started to popular the bank, using the Entity Framework, became a mess and I began to see that the Entity Framework got it all wrong rs (EF donkeys)...

I know the way that Mapeie is incorrect, but I’m not getting how I can be mapping the classes correctly.

Edit:

The RU has created the following tables:

Task

Id          Nome
1           Lavar Louça
2           Limpar Casa       

Taskmaster

Treafa_Id        DiaSemana_Id
1                2
2                2

Days

Id             Dia
1              Segunda
2              Terça
3              Quarta

Daycare

DiaSemana_Id         Cliente_Id
2                    99
2                    66

Client

Id        Nome
99        Diego
66        Felipe

In this case the bank is assuming that Diego and Felipe have both tasks. However Diego Tuesday has only the task of Dishwashing and Felipe only the task of Cleaning House.

In my understanding needed the id client in the table Tarefadiasemana

  • I didn’t understand your problem. The mapping is correct. What would be wrong?

  • @Gypsy omorrisonmendez I edited the question, it became clearer the problem now.

  • Ah, now that I’ve seen it. My lack of attention. I’ll answer.

1 answer

3


This is wrong here:

public class DiaSemana
{
    public DiaSemana()
    {
        Tarefas = new List<Tarefa>();
        Clientes = new List<Cliente>();
    }

    ...

    public virtual ICollection<Tarefa> Tarefas { get; set; }

    public virtual ICollection<Cliente> Clientes { get; set; }
}

First of all, this doesn’t need:

    public DiaSemana()
    {
        Tarefas = new List<Tarefa>();
        Clientes = new List<Cliente>();
    }

Entity Framework initializes navigation properties for you. This code makes no difference to the application.

Second:

public class DiaSemana
{    
    ...

    public virtual ICollection<Tarefa> Tarefas { get; set; }

    public virtual ICollection<Cliente> Clientes { get; set; }
}

If DiaSemana is the entity that associates Tarefas and Clientes, she can’t have N Tarefas and N Clientes, after all, it associates only one Tarefa with only one Cliente.

Switch to the next:

public class DiaSemana
{    
    [Key]
    public int DiaSemanaId { get; set; }
    // Importante incluir os campos de chave estrangeira também!
    [Index("IUQ_DiaSemana_TarefaId_ClienteId", IsUnique = true, Order = 1)]
    public int TarefaId { get; set; }
    [Index("IUQ_DiaSemana_TarefaId_ClienteId", IsUnique = true, Order = 2)]
    public int ClienteId { get; set; }
    ...

    public virtual Tarefa Tarefa { get; set; }
    public virtual Cliente Cliente { get; set; }
}

[Index], introduced in this form from the Entity Framework 6.1.0, ensures the uniqueness of the associative record. Additional validations may be required in the application to avoid strange key duplicity errors for the user.

  • So for me to be recovering all the tasks of a particular client would be: var tarefas = cliente.DiasSemana.Where(x=>x.Dia == "Segunda").ToList();

  • Yeah, actually, considering by day, it would be: var tarefas = cliente.DiasSemana.Where(x=>x.Dia == "Segunda").SelectMany(x => x.Tarefas).ToList();

  • Show... I’d better create maybe a enum not to get crowded the bank with a lot of string like "Monday", "Tuesday", but as 1, 2, 3...

  • Yeah, that’s what I’d wear if I were you.

Browser other questions tagged

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