How to create a template with just a few table fields?

Asked

Viewed 116 times

3

I have some tables with several fields. For example, the table (X) has some 20 fields of which I will only use 2 or 3.

In this situation I have to create the templates with all fields and ignore the properties on Entitytypeconfiguration or is there any way for me to map only the fields my application needs in my models?

  • 2

    You are using Codefirst or Databasefirst?

2 answers

3


The easiest way I found to solve this problem was by creating a view and configuring via Fluent Api that my entity relies on this view and not on the table itself.

In the configuration in the table where instead of informing the table the view should be informed:

ToTable("VIEW_USUARIO", "Schema_Data_Base");
  • 3

    And what else? How was this configuration?

  • In the configuration ta table where instead of infornating the table should be informed the view: ToTable("VIEW_USUARIO", "Schema_Data_Base");

  • Edit your reply with this part. Here in the comments there is no code formatting.

3

Using ORM, it is always complicated to define more than one entity for the same table.

But because of this limitation we end up discovering that we don’t need to do this; that doing so increases the complexity of the system unnecessarily.

You need some read-only fields?

Select (LINQ) a new object type (eventually anonymous) and return only these fields.

You need to read and also change these few fields?

Maybe there is a conceptual error.

Only the entity knows the complexity of its state changes. For example: Can you even change two attributes of an entity without it having the value of other attributes to validate itself?

On the other hand, you as a programmer are a great connoisseur of the domain and suddenly know that you can make this change without risk, and consider important the performance gain of not having to rescue the entire entity to make this small change.

In this case, you can make the changes via UPDATE commands, giving up using the entity definition. This ensures the performance and makes explicit that the change is being made without the knowledge of the entity’s complete state, because it was not redeemed from the bank.

Using this option, you may want to define a repository API for this entity instead of always manipulating it directly through LINQ or direct commands, so as to centralize in an object the persistence and recovery of this type of entity, what can improve the overall expressiveness of the code and the reuse of custom methods of persistence and recovery.

  • The easiest way I found to solve this problem was by creating a view and configuring via Fluent Api that my entity relies on this view and not on the table itself.

Browser other questions tagged

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