How to make an M:N autorelationation in the Entity Framework?

Asked

Viewed 28 times

0

Good afternoon, I need to self-relatify and I’m really confused how I’m supposed to do this. In my system I have several process variables, a same process variable can be Father of several other variables, but this same variable can also be daughter of other variables. I saw some ways to self-relatify, but none of them seemed "right" to my case.

public class VariavelProcesso
{
 public int VariavelProcessoId { get; set; }

 public int? VariavelPaiId { get; set; }
 public virtual VariavelProcesso VariavelPai { get; set; }
 public string Nome { get; set; }
}
  • The title says one thing and the text says another.

1 answer

-1


What version of Entityframework are you using? Using . Net Framework or already . Net Core?

Considering the use of EF Core 2.x or higher

public class VariavelProcesso
{
    public int VariavelProcessoId { get; set; }
    public int? ParentVariavelProcessoId { get; set; }
    public string Nome { get; set; }

    public virtual VariavelProcesso Pai { get; set; }
    public virtual HashSet<VariavelProcesso> Filho { get; set; }   
}

//Fluente API

modelBuilder.Entity<VariavelProcesso>()
    .HasOne(x => x.Pai)
    .WithMany(x => x.Filho)
    .HasForeignKey(x => x.VariavelProcessoId);

Browser other questions tagged

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