EF Core Relationships one-to-many-to-many FluentAPI

0

Good afternoon, I would like to ask a relationship question in Entity Core. I have a person table and the person has a position (Manager, supervisor, responsible, etc ...)

public class Pessoa
{
    public int PessoaId { get; set; }
    public string Nome { get; set; }
    public int CargoId { get; set; }
    public virtual Cargo { get; set; }
}
public class Cargo
{
    public int CargoId { get; set; }
    public string Nome { get; set; }
    public virtual ICollection Pessoas { get; private set; }
}

So far, I have no doubts.

Now let's assume that I have a Room table and this room has a Manager, a supervisor and a person in charge, who are People. What would the relationship look like?

So:

public class Sala
{
    public int SalaId { get; set; }
    public string Nome { get; set; }
    public int ResponsavelId { get; set; }
    public virtual Pessoa Responsavel {get; set; }
    public int SupervisorId { get; set; }
    public virtual Pessoa Supervisor {get; set; }
    public int GerenteId { get; set; }
    public virtual Pessoa Gerente {get; set; }

}

And would change Person

public class Pessoa
{
    public int PessoaId { get; set; }
    public string Nome { get; set; }
    public int CargoId { get; set; }
    public virtual Cargo { get; set; }

    public virtual ICollection Salas { get; private set; }
}

And you would create the relationship in each field in the Fluent API with the Salas property or

public class Pessoa
{
    public int PessoaId { get; set; }
    public string Nome { get; set; }
    public int CargoId { get; set; }
    public virtual Cargo { get; set; }

    public virtual ICollection SalaResponsavel { get; private set; }
    public virtual ICollection SalaSupervisor { get; private set; }
    public virtual ICollection SalaGerente { get; private set; }
}
    
asked by anonymous 28.10.2017 / 21:27

2 answers

0

In this form you are doing, it would look like this:

 public virtual Sala SalaResponsavel { get; private set; }
 public virtual Sala SalaSupervisor { get; private set; }
 public virtual Sala SalaGerente { get; private set; }

But, I think it gets a bit messy, I think it would make another table containing

Sala
Pessoa
Cargo

So the relation would be n-n

I'm on my cell now, at night I try to complement the answer more.

    
28.10.2017 / 21:54
0

For my context the answer from the link below was the solution.

  

link

I made the mapping and the entity loaded without problem. Now with Dapper fill in the data. Thank you.

    
30.10.2017 / 19:11