What are the good practices of MVC with Entity Framework?

Asked

Viewed 84 times

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 which folder would be ideal (good practice), create the Entity Framework?

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

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

  • Ideal would be for you to create your DDD application. That’s all.

  • Em qual pasta seria ideal (boas praticas), criar o Entity Framework? What do you mean, create what? This sentence makes no sense. Na pasta MODEL, ou eu posso criar uma pasta chamada Context fora da pasta MODEL? Yes, the question is whether you should, and it depends on what you want to do, what you don’t have in the question. Pois dentro do Entity Framework criado contém as classes do banco seria dentro do model, não é ? Probably. The question is not very clear. So the current answers end up not making much sense either.

3 answers

0

Yes I would put in Models.

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

0

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

In the project MVC put the models you will use in the views, even if they are the same as the models in the EntityFramework. Why?

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

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

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

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

0

Subdirectories are always an elegant way to organize your code.

As with data, it is important to stay inside the Model, obeying the MVC architecture. In the example, I distinguish files that are Entity and ADO. Within an area (module) I call Quality.

inserir a descrição da imagem aqui

Browser other questions tagged

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