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;}
}