Relationship ASP.NET

Asked

Viewed 44 times

-2

Good afternoon I have a question about a relationship.

i have 2 classes: Internal Person and Unit.

i have the following properties in Internal Person:

inserir a descrição da imagem aqui

Personal internal belongs to a single unit, but in Unit we have:

inserir a descrição da imagem aqui

in unity, one person can be the manager of another.

In other words, at the same time that a person belongs to a unit, she can also be the manager of another unit.

I tried to make the following relationship using Fluent api:

inserir a descrição da imagem aqui

But this mistake happens:

inserir a descrição da imagem aqui

I know I can’t use 2 navigation attributes with the same classes. What would be the most practical solution?

  • hello Luan, avoid images and put the text is better to view

1 answer

0


Following its line of reasoning, an internal Ncsoaint may be related in one Ncunit as a person Terna and the other Ncunit as a manager. So that means that the internal Ncpeopleclass can have more than 1 Ncunit and Ncunit can have more than 1 internal Ncpeople1, whether you are the manager or not.

Then we have an N-N relationship between the classes, so a new class should be created that calls table-relatedness, it will make the relationship between the Inner Person and Unity.

In Ncpesint change the Unit property:

public List<NcPessoaInternaUnidade> Unidades { get; private set; }

In Ncunidade exchange the Personal and Management properties for:

public List<NcPessoaInternaUnidade> PessoaInternas { get; private set; }

Create the relationship class:

public class NcPessoaInternaUnidade {
  public int Id { get; set; }
  public NcPessoaInterna PessoaInterna { get; set; }
  public int PessoaInternaId { get; set; }
  public NcUnidade Unidade { get; set; }
  public int UnidadeId { get; set; }
  
  public bool Gestor { get; set; }
}

Use the boolean Manager property to declare if Ncpesintern is a manager (true) or if it is an internal person (false) in an Ncunit.

Redo fluent API this way:

//Relacionamento NcPessoaInternaUnidade - NcPessoaInterna
modelBuilder.Entity<NcPessoaInternaUnidade>()
        .HasOne(p => p.PessoaInterna)
        .WithMany(b => b.Unidades)
        .HasForeignKey(p => p.PessoaInternaId);

//Relacionamento NcPessoaInternaUnidade - NcUnidade
modelBuilder.Entity<NcPessoaInternaUnidade>()
        .HasOne(p => p.Unidade)
        .WithMany(b => b.PessoaInternas)
        .HasForeignKey(p => p.UnidadeId);

I hope I’ve helped.

Browser other questions tagged

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