Declare Entity Framework Relationship one for many

Asked

Viewed 218 times

0

I have two classes:
Person

public class Pessoa
{
    public int Codigo { get; set; }
    public string Nome { get; set; }
     ....

    public virtual ICollection<Endereco> Endereco { get; set; }
}

Address

public class Endereco
{
    public int Codigo { get; set; }

    public int Cep { get; set; }
    public string Logradouro { get; set; }
    public string Quadra { get; set; }
    public string Lote { get; set; }
    public string Complemento { get; set; }
    public string Bairro { get; set; }
    public string Cidade { get; set; }

    //chave estrangueira
    public int PessoaId { get; set; }
    public virtual Pessoa Pessoa { get; set; }
}

but I’m having difficulty making this relationship in Onmodelcreating Both those and others, from that I will be able to make the others

my Onmodelcreating

 // Uma pessoa tem vários endereços
 modelBuilder.Entity<Pessoa>()
   .HasOptional(p => p.Endereco)
   .WithMany(e => 

What would modelBuilder look like in this relationship?

1 answer

1


Would that be

modelBuilder.Entity<Pessoa>
            .HasMany(p => p.Enderecos)
            .WithRequired() // Porque todo endereço precisa pertencer a uma Pessoa
            .HasForeignKey(p => p.PessoaId);

Browser other questions tagged

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