Mapping of Associative Tables with Entity Framework

2

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

Example:

  

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

Map 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 started to popular the bank, using Entity Framework , it became a mess and I started to see that Entity Framework understood everything wrong rs ( EF donkey rs) ...

I know the way I map is incorrect, but I can not figure out how I can map the classes correctly.

Edit:

EF has created the following tables:

Task

Id          Nome
1           Lavar Louça
2           Limpar Casa       

Weekday Task

Treafa_Id        DiaSemana_Id
1                2
2                2

DiaSemana

Id             Dia
1              Segunda
2              Terça
3              Quarta

DiaSemanaCustomer

DiaSemana_Id         Cliente_Id
2                    99
2                    66

Customer

Id        Nome
99        Diego
66        Felipe

In this case the bank is assuming that Diego and Felipe have both tasks. But Diego de Terça has only the task of Washing Dishes and Felipe only the task of Clean House.

In my understanding, I needed the client id in the TaskTableDaysheet

    
asked by anonymous 02.09.2015 / 03:11

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, this does not need:

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

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

Secondly:

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 , it can not have N Tarefas and N Clientes , after all, it associates only Tarefa with only Cliente .

Change to the following:

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, guarantees the uniqueness of the associative register. Additional validations may be required in the application to avoid extraneous errors of key duplication for the user.

    
02.09.2015 / 16:04