Dariusz Tarczynski
Dariusz Tarczynski

Reputation: 16711

Where to put Entity Framework Data Model in MVC application?

Lets consider default ASP.NET MVC application folder structure, so it's looks like this:

-App_data
-Content
-Controllers
    HomeController.cs
-Models
    AccountModels.cs
-Scripts
-Views

My question is: Where is the best place to put Entity Framework Data Model (EDMX) file? Is it Models folder? Yes - we know that good solution is to introduce new Project and reference it to MVC application, but lets forget about this now.

Upvotes: 15

Views: 10810

Answers (4)

Marc
Marc

Reputation: 4813

I would put the EF-model (aka physical model) always in its own assembly or in a "core" assembly outside of main MVC application. The same applies for your business-logic / domain-logic / domain-services / etc. Separate the non-web stuff from the MVC-Web-Application.

This will help you re-use the core part of your app. For example when you need to expose it as a service, a command-line tool, migration-tool, etc.

Because storing this in its own assembly is so easy and takes you a few minutes I highly recommend doing this for each and every tiny app too.

Upvotes: 1

Kristóf Tóth
Kristóf Tóth

Reputation: 821

My opinion is that you should create

  1. a separate project for domain objects, datacontracts etc. etc... Like MyProject.Infrastructure including many folders like DataContracts, Model, Exceptions etc.
  2. a separate project for DataAccess wich contains the DBContexts and the Repositories, this way you can easily manage migrations later on

Upvotes: 0

RPM1984
RPM1984

Reputation: 73112

Well this is debatable, but i'd vote +1 for the Models folder.

The only other candidate would be App_Data, but this is generally for file-based databases (SQL Server CE .MDF, for example) and files you don't want served by IIS.

As the EDMX is an abstraction of the database, it should go into the Models folder.

If the project gets bigger, you should definetely move your EF Model into another project. To future-proof yourself from this, make your Controllers access the EDMX via Repository/Interfaces, so when you move the DAL over to another project, all you'll have to do is add the reference and add in the using statements.

Upvotes: 8

George Stocker
George Stocker

Reputation: 57877

For a small project, it should be part of the Model. For a larger product, the repository and the associated model could be in a separate assembly.

Upvotes: 19

Related Questions