2
There was a doubt of inheritance modeling in C# involving the entities Pessoa, Pessoafísica and Pessoalegal, but Gypsy Morrison Mendez helped a lot! Now I have another question in another type of relationship. As I said, a person can be physical or legal. A person can still be a customer and/or supplier. And the customer can have related services. Follow the modeling:
The models in the code stayed:
[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 worked well... the interaction with the bank for registration, editing and consultation is great. However, when it comes to registering the services I have a flea behind my ear. Conceptually speaking. I connected the 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 a Personal Physique is an inherited class of Person, just as Personal is an inherited class of Person. Therefore, if I had a table for people’s addresses, in the address table I would use the key from the Person table to maintain the relationship. A person will always be OR personal OR personal.
Now there is another question... from the Supplier and Customer. A person MAY OR MAY NOT BE A SUPPLIER... MAY OR MAY NOT BE A CUSTOMER. And it may be both or it may not be either.
Although a customer or supplier ALWAYS is a person, the Service entity may only be associated with a Customer and never with a supplier (at least in this application).
Here comes the first question... for this desired concept, this modeling is correct? The keys in the tables, cardinality... this is right?
Second doubt...
I understand that in the Service table the relationship key idClient should contain the primary key of the Client table, not the primary key of the Pessoa table. THAT CONCEPT IS CORRECT?
Following the valuable help of Gypsy, I set up these classes and in the service registration controller filling out the customer list was like this:
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, but when registering in the database, the idClient field of the service table receives the idPessoa field of the person table. And it’s making me uncomfortable...
Should not the idClient field of the service table have the idClient field, which is the primary key of the Client table? If so, how should I adjust my classes to make it work?
Thank you all for your help!
It’s weird in there. How’s the View?
– Leonel Sanches da Silva
I put the view, Gypsy! I think the service class that has something. When I followed your suggestion, in the client class did not have the attribute idCliente. Only idPessoa with the notation [Key, Foreignkey("person")]. But to make the customer relationship 1 : no service need to have the idClient in the service, correct? But if you don’t have the client class, it doesn’t work... are these mappings correct? Thank you for the strength!
– Felipe Bulle