Entity Framework Design - Several Data Views

I have a design question related to Entity Framework objects.

I created the following object:

public class SomeEntity { // full review details here } 

This object has 30 columns as an example. When I need to create a new object, this works fine. I have all the necessary fields to insert into the database.

I have several places in my application where I need to display some tabular data with some fields from SomeEntity, but I do not need all 30 columns, maybe only 2 or 3 columns.

Am I creating a completely new object that has only the fields that I need (which map to the same table as SomeEntity, but only retrieve the desired column?)

Or it makes sense to create a domain class (for example, PartialEntity) and write a query like this:

 var partialObjects = from e in db.SomeEntities select new PartialEntity { Column1 = e.Column1, Column2 = e.Column2 }; 

I do not know how to do that. Is it nice to have two entities that map to the same table / columns? I would never need to create a PartialEntity and store it in the database, because it would not have all the fields that are required.

+6
source share
4 answers

Your first approach is impossible. EF does not support multiple objects mapped to the same table (except for some special cases, such as TPH inheritance or table splitting).

The second case is a general scenario. You will create a presentation model for your user interface and either ask your object to view the model directly in the query (it will be transferred from only the columns of the DB database), or you will query the whole entity and make a transformation to view the model in your application code (for example, AutoMapper, as @Fernando mentioned).

If you use the EDMX file for matching (I think you do not do this because you mentioned ef-code-first ) you can use the third approach, which takes part in both mentioned approaches. This approach defines QueryView , an EF-based view for a mapped object that behaves like a new read-only object. As a rule, this is a reusable projection stored directly in the mapping.

+2
source

What you proposed as the first solution is the “model view paradigm”, where you create a class with the sole purpose of being a view model to retrieve data, and then map it to the model class. You can use AutoMapper to map values. Here's an article on how to apply this.

+2
source

You can create a general property filter that takes an instance of an object, and you pass an array of strings from the column names, and this method will return a dynamic object with only the desired columns.

+1
source

I think this will add unnecessary complexity to your model in order to add a second entity based on the same data structure. I honestly do not see a problem in that I have the only entity for updating / editing \ viewing. If you insist on sharing access to SomeEntity, you can have a database view: for example, SomeEntityView and create a separate object based on it.

+1
source

Source: https://habr.com/ru/post/899001/


All Articles