Entity Framework 6 relationship

Asked

Viewed 78 times

1

I have a class where I need to have two different relationships with the same table.

public class Usuario
{
    public int Naturalidade {set;get;}
    public int CidadeEndereco {set;get; 
}

public class Cidade
{
   public int Id {set;get;}
   public string Nome {set;get;
}

How do I do this relationship? It can be using Fluent API or Data Annotation?

  • Here is your answer https://msdn.microsoft.com/en-us/data/jj713564.aspx

1 answer

3


You treat normally, as if they were separate entities.

public class Usuario
  {

    public int NaturalidadeId {set;get;}
    public int CidadeEnderecoId {set;get;

     [ForeignKey("NaturalidadeId ")]
     public virtual Cidade Naturalidade{ get; set; }
     [ForeignKey("CidadeEnderecoId ")]
     public virtual Cidade CidadeEndereco{ get; set; }
}

public class Cidade
{

   public int Id {set;get;}
   public string Nome {set;get;

}

Browser other questions tagged

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