What are the good practices of MVC with Entity Framework?

0

I would like to know what are the good practices of MVC using the Entity Framework, regarding the MVC folders.

When I create the Entity Framework, it automatically creates the database file, and the Context file.

In what folder would be ideal (good practices), create the Entity Framework?

In the MODEL folder, or can I create a folder called Context outside the MODEL folder?

Because within the Entity Framework created contains the classes of the bank would be within the model, is not it?

    
asked by anonymous 05.07.2018 / 11:26

3 answers

0

Yes I would put it on Models.

Or in a class library if it were common to more than one project

    
05.07.2018 / 11:40
0

It would be a good practice to separate the data model from the display model. To do this, create a project with only the bank part, any known model / pattern (DAC, Repository, etc) and put the models there.

In the MVC project, place the models you are going to use in the views, even if they are the same as the models in EntityFramework . Because?

Imagine that you have a model for inputting data into a form, and you want to add validation, DataAnnotations to that model for example. You can do this freely in this model without having to change the EntityFramework model.

If for example you have a field in the database for documents, CPF, CNPJ, RG, etc. which are numeric types. In the view you want to use a mask for points, traces, bars, etc, and want to use a string attribute, how would you do? Separating the models would be no problem.

Here are just some situations where it is recommended to separate the data model from the display model, so I suggest leaving the models that exclusively serve the Views, and the database data models in another project in the MVC Models folder.

An example would be:

ProjetoDados
   Models
       ... aqui vão as models do EntityFramework

ProjetoMVC
   Controllers
   Views
   Models
       ... aqui vão as models para atender as Views
    
05.07.2018 / 13:07
0

Subdirectories are always a neat way to organize your code.

As a result of data, it is important that they stay within the Model, obeying the MVC architecture. In the example, I distinguish files that are Entity and ADO. Within an area (module) I call Quality.

    
05.07.2018 / 18:27