4
I am training in class mapping for the Entity Framework using the Fluent API and I have some questions.
The first is as follows: Map a Foreign key with attributes would be like this:
public int EmpresaId {get; set;}
[ForeignKey("EmpresaId")]
public Empresa Empresa {get; set;}
With Fluent API it would be like this:
HasRequired(p => p.Empresa).WithMany().HasForeignKey(p => p.EmpresaId);
Is that correct? What I don’t understand is this WithMany()
since it is a simple foreign key connection and is not a ICollection<>
, for example. So why WithMany()
?
The other question would be, how would the mapping with 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;}
}
Gosh, I don’t even have time to answer you appear here already responding ein... kkk will be active so there in china... joke...
– Tafarel Chicotti
Cool your class example
abstrata
ofPessoa
, but as for the property Name, it was seenFluent API
i want to specify that it is required and has a maximum of 70 characters, for example, where I would make this specification?– user3628
@Tiago I’ll edit the answer for you.
– Leonel Sanches da Silva
@Gypsy omorrisonmendez but he could make one
DbSet<Pessoa>
and in themodelBuilder.Entity<Pessoa>.Property(p => p.Nome).IsRequired();
He could do the validation, couldn’t he?Descriminator
the framework would handle distributing the classes correctly, wouldn’t it? (ps: can’t test now [away from a visual studio/. net PC])– Tafarel Chicotti
@tchicotti Pode. Also worth as an example.
– Leonel Sanches da Silva