EF 6, Mapping with Fluent Api in classes with inheritance

4

I am training the class mapping for the Entity Framework using the Fluent API and I have some questions.

The first is this: Mapping a foreign key with attributes would look like this:

public int EmpresaId {get; set;}
[ForeignKey("EmpresaId")]
public Empresa Empresa {get; set;}

With the Fluent API it would look like this:

HasRequired(p => p.Empresa).WithMany().HasForeignKey(p => p.EmpresaId);

Correct this? What I did not understand is this WithMany() since it is a simple foreign key binding and is not a ICollection<> , for example. So why give WithMany() ?

The other question would be, how would you map the Fluent API of these classes, since two inherit from the first?

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

public class PessoaFisica : Pessoa
{
    public string CPF {get; set;}
}

public class PessoaJuridica : Pessoa
{
    public string CNPJ {get; set;}
}
    
asked by anonymous 19.03.2014 / 15:56

2 answers

6

WithMany

WithMany() is used to specify a Many relationship. What's really weird is that you're using WithMany() with no arguments. WithMany() is for you to specify the inverse relation.

For example, if that snippet of code refers to a person, the correct one would be mapping like this:

HasRequired(p => p.Empresa)
    .WithMany(e => e.Pessoas)
    .HasForeignKey(p => p.EmpresaId);

Person Class

A Pessoa in theory can only be Physical or Legal, right? The correct thing would be to do this:

public abstract class Pessoa
{
    public int Id {get; set;}
    public string Nome {get; set;}
}

public class PessoaFisica : Pessoa
{
    public string CPF {get; set;}
}

public class PessoaJuridica : Pessoa
{
    public string CNPJ {get; set;}
}

In Fluent API:

modelBuilder.Entity<PessoaFisica>()
    .HasKey(pf => pf.Id);
modelBuilder.Entity<PessoaFisica>()
    .Property(pf => pf.Nome)
    .HasMaxLength(70)
    .IsRequired();
modelBuilder.Entity<PessoaJuridica>()
    .HasKey(pj => pj.Id);
modelBuilder.Entity<PessoaJuridica>()
    .Property(pj => pj.Nome)
    .HasMaxLength(70)
    .IsRequired();
    
19.03.2014 / 16:38
3

James, probably your class should look something like this:

public Pessoa {
    public int Id { get; set; }
    public int EmpresaId {get; set;}
    [ForeignKey("EmpresaId")]
    public Empresa Empresa {get; set;}
}

Your Enterprise class should be something around

public Empresa {
    public int Id { get; set; }
    //... 
    public ICollection<Pessoa> Pessoas {get; set;}
}

Am I right? If so, your Fluent relationship should look like this:

modelBuilder.Entity<Pessoa>()
            .HasRequired(p => p.Empresa)
            .WithMany(e => e.Pessoas)
            .HasForeignKey(p => p.EmpresaId);

What happens is as follows: The person Entity<Pessoa> has a required field (required) HasRequired(p => p.Empresa) that is part of a company list WithMany(e => e.Pessoas);

That is, this person needs to have a company and that person will be linked to the people that exist in that company ( ICollection ).

    
19.03.2014 / 16:41