Click here to Skip to main content
12,829,014 members (38,325 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET MVC MVC3
Where we will write the code for accessing datas from database. i mean which is directly link to database, "Controller" or "MOdel" in mvc


Posted 9-Aug-12 4:15am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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)
(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).
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.


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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170326.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100