Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Where we will write the code for accessing datas from database. i mean which is directly link to database, "Controller" or "MOdel" in mvc



Thanks,

Dileep
Posted

You should learn MVC in detail (I advise you to read a book), if you are coming from event driven programming world then it will take a while to understand MVC concept. following is a quick overview.

Model - is a wrapper to your database, mostly using an ORM (Object Relation Mapping) i.e. LINQ or Entity Framework, you could write Web Services or Simple data access Classes to access the database using Model.

View - is how the application will look to user, user will not see Controller or Model, user will interact with your application using View. View contains HTML, Razor, Javascript/jQuery, UI Elements etc..

Controller - is the middle man between Model and View, it will send command to Model from the resulting View actions. i.e. if user clicks "Save" button on View Controller will send command to Model to execute "Update/Insert" action.

MVC Relationship
----------------
 (user side)
    View
      |
      |
 Controller
      |
      |
    Model
(database side)


Now answer to your question, if you are already using Entity Framework / LINQ and Model is already in place. then a quick way is to call database using LINQ Expressions inside a Controller to manipulate Model. otherwise you need to specify your database manipulation methods under Model, and then call those methods inside Controller.

so Model will directly link to database, and if you do not want to write those traditional data access code, use ORM (like Entity Framework or LINQ).
 
Share this answer
 
Most people will suggest using the "Repository Pattern" to move that data access code out of the controller. Here is a link[^] which gives an example and references. I have created projects using both approaches but I think having Repository classes is a clean solution.
 
Share this answer
 
Hi,
You should read these article to clear the confusion:
Architecture Guide: ASP.NET MVC Framework + N-tier + Entity Framework and Many More[^]
N-Tier development with ASP.NET MVC, WCF, and LINQ[^]

Here[^] you go for the similar thread.


--Amit
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900