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

Asked

Viewed 183 times

2

I’m starting to study LINQ to try to implement a project I’m working on. For examples both 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 particularities.

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

2) The modeling of my database works as follows. I have a table called Clientes which is what I call "daughter table" from another table called Pessoas. Explaining, the table Pessoas plays the role of a parent table, which stores part of the data from other tables such as Clientes, Funcionarios, etc. That is, fields that are in common in these tables like (Name, Email, Phone, etc.) are all stored in the main table Pessoas. Following the concept of OOP heritage. In the class modeling I did, Pessoa is an abstract class, that is, it cannot exist alone, it is only to serve as a base class for concrete classes Cliente and Funcionario. So when a Cliente part of the data must be stored in the table Pessoas and the other part in the table of Clientes How will LINQ handle this? Because if I were to generate the classes from the tables, LINQ would create a concrete class for the table Pessoas but in my UML modeling Pessoa is an abstract class.

1 answer

0

Basically, inheritance mapping in LINQ to SQL can be done according to this tutorial. It is necessary to define a column as discriminator on the parent table. This parent table will have all parent entity and derivative entity fields, and derived entity fields are all optional to be populated. The processing of the validation of these fields is done by the application.

Heritage 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 in the answers below:

  • Still about an option via LINQ, I found that. Only I didn’t understand it very well. These properties that the author of the answer uses in the example are already ready in LINQ?

  • It uses exactly how the Entity Framework works, using a data context (which is an implementation of a Unit of Work) and calling a DbSet performing the selection or persistence operations. I think it is worth it to go after the tutorials of Entity Framework because your solution is already targeting the use of it.

  • Those decorators [Table(Name = "AnyName")], [Column], [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)], etc. are part of the LINQ or were created by the author?

  • I’ve been reading the tutorial you posted about using inheritance with LINQ. However, in my modeling, the base class is abstract. This will be a problem to implement the solution described in the tutorial?

  • About the decorators, they are part of the .NET. Not only the LINQ framework. About the second, I believe that not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.