Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 and Entity Framework

My question is pretty simple: is it a good practice to place the .edmx file in the model folder in a Web application of an MVC3 project?

like image 766
Jorge Avatar asked Sep 19 '11 16:09

Jorge


People also ask

What is difference between MVC and Entity Framework?

MVC is framework mainly concentrates on how you deliver a webpage from server to client. Entity framework is an object relational mapper which helps you to abstract different types of databases (MSSQL,MySQL etc) and helps querying objects instead of having sql strings in our project.

Can we use Entity Framework in VB net?

Attachments: Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is mvc3 application?

ASP.NET MVC 3 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the . NET Framework.

Can we use Entity Framework in MVC?

The latest version is Entity Framework 6.0. We are using it in our ASP.NET MVC application. First, we will create a project then adding models to it.


2 Answers

my answer is pretty simple, do not mess up presentation layer (Whole MVC application) with data access logic and data modeling.

Have at minimum 4 projects in your Visual Studio Solution, from bottom to the top:

1 - ProjectName.Interfaces (Class library, entities's interfaces);

2 - ProjectName.DAL (Class library, the only one allowed to even know the EF is used, the POCO entities implement the interfaces of project 1 using another file where you redeclare same objects using partial classes...);

3 - ProjectName.BL (Class library, Business logic, references the two projects above 1 and 2);

4 - ProjectName.Web (ASP.NET MVC application, Presentation Layer, references two projects 1 and 3 but NOT 2);

this to simplify things of course, based on my experience this is a solid design, a bit overkilling for very small projects but pays off in the long term.

in my view, the M of MVC, Model, is NOT the data model, is not the EF, is not there to do ORM bound to the specific database engine.

this answer is subjective of course and is based on my personal experience ;-)

like image 127
Davide Piras Avatar answered Sep 20 '22 07:09

Davide Piras


I agree with Davide here completely I just want to add that you should also consider using the POCO templates to generate poco objects and not return entity framework objects to another layer because it then puts a dependency on the entity framework.

At some inevitable point if you don't pluck this out into a separate project, your direct data access code ends up thrown into your web code. I see it all the time (and we've all been guilty of it at some time)

like image 32
Adam Tuliper Avatar answered Sep 20 '22 07:09

Adam Tuliper