I was having a question of inheritance modeling in C # involving Person, Individual, and Legal entity, but Cigano Morrison Mendez helped a lot! Now I have another question in another type of relationship. As I said, a person may be physical or legal. A person can still be a customer and / or supplier. And the customer may have related services. Here's the modeling:
Thetemplatesinthecodewere:
[Table("pessoa")]
public class pessoa
{
[Key]
public int idPessoa { get; set; }
[Required]
[StringLength(90)]
public string nomeRazaoSocial { get; set; }
public virtual fornecedor fornecedor { get; set; }
public virtual cliente cliente { get; set; }
}
[Table("pessoafisica")]
public class pessoafisica : pessoa
{
[StringLength(15)]
public string cpf { get; set; }
}
[Table("pessoajuridica")]
public class pessoajuridica : pessoa
{
[StringLength(45)]
public string cnpj { get; set; }
}
[Table("cliente")]
public class cliente
{
[Key, ForeignKey("pessoa")]
public int idPessoa { get; set; }
public virtual pessoa pessoa { get; set; }
}
[Table("fornecedor")]
public class fornecedor
{
[Key, ForeignKey("pessoa")]
public int idPessoa { get; set; }
public virtual pessoa pessoa { get; set; }
}
So far everything has worked well ... the interaction with the bank for registration, editing and consultation is fine. However, at the time of registering the services I have a flea behind my ear. Conceptually speaking. I related customer service and my class stayed:
[Table("servico")]
public class servico
{
[Key]
public int idServico { get; set; }
[ForeignKey("cliente")]
public int idCliente { get; set; }
public string descricao { get; set; }
public virtual cliente cliente { get; set; }
}
Clearly an Individual is a class inherited from Person, just as PersonJuridica is a class inherited from Person. Soon, if I had a table for people's addresses, in the address table I would use the Person table key to maintain the relationship. A person will always be OU personal or personal.
Now you have another question ... of the Supplier and Customer. A person MAY OR NOT be a supplier ... also MAY or NOT be a customer. And it can be either of them or not.
Although a customer or supplier is ALWAYS a person, the Service entity can only be associated with a Customer and never with a provider (at least in this application).
Here comes the first doubt ... for this desired concept, is this modeling correct? The keys in the tables, cardinality ... is that right?
Second doubt ...
I understand that in the Service table the idClient relationship key should contain the primary key of the Customer table, not the primary key of the Person table. IS THIS CONCEPT CORRECT?
Following the valuable help of Cigano, I set up these classes and in the controller of the service register the filling of the list of clients was as follows:
ViewBag.idCliente = new SelectList(db.cliente
.Include(c => c.pessoa)
.Select (c => new
{
idCliente = c.idPessoa,
nomeRazaoSocial = c.pessoa.nomeRazaoSocial
}).ToList(), "idCliente", "nomeRazaoSocial");
The application works, however when registering in the database, the idClient field of the service table receives the Person id field of the person table. And this is making me uncomfortable ...
The idClient field of the service table should not have the idClient field, which is the primary key of the Customer table? If so, how should I adjust my classes for this to work?
Thank you all for the help!