LINQ to SQL with entities that already exist, inheritance and composition

2

I'm starting to study LINQ to try to implement a project I'm working on. For examples in articles as in videos, LINQ has an interface in Visual Studio that generates the entity classes from the tables in the database. However, in my application project there are some peculiarities.

1) My entity classes are already created, so I want to be able to use them with LINQ.

2) Modeling my bank works as follows. I have a table named Clientes which is what I call "daughter table" from another table named Pessoas . Explaining, the Pessoas table plays the role of a parent table, which stores some of the data from other tables as Clientes , Funcionarios , and so on. That is, fields that are common in these tables as (Name, Email, Phone, etc.) are all stored in the main table Pessoas . Following the concept of heritage of the OOP. In class modeling I have done, Pessoa is an abstract class, that is, it can not exist by itself, it is just to serve as the base class for the concrete classes Cliente and Funcionario . So, when a Cliente is inserted into the database part of the data must be written to the Pessoas table and the other part to the Clientes How will LINQ handle this? For if I were to generate the classes from the tables, LINQ would create a concrete class for the Pessoas table but in my UML modeling Pessoa is an abstract class.

    
asked by anonymous 15.04.2016 / 00:29

1 answer

0

Basically, inheritance mapping in LINQ to SQL can be done conforming this tutorial . You must define a column as discriminator in the parent table. This parent table will have all the fields of the parent entity and also of the derived entities, with the fields of the derived entities being all optional to be filled. The validation treatment of the filling of these fields is done by the application.

Inheritance and composition are classics of data modeling, and Microsoft has developed another framework called Entity Framework that solves these cases very well. I exemplify how to do it in the answers below:

15.04.2016 / 22:42