Click here to Skip to main content
Rate this: bad
good
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
 

 
Thanks,
 
Dileep
Posted 9-Aug-12 3:15am
dilzz1.2K
Rate this: bad
good
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)
    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).
  Permalink  
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 448
1 Sergey Alexandrovich Kryukov 290
2 PhilLenoir 95
3 Uma Shankar Patel 80
4 CB Sharma 79
0 Sergey Alexandrovich Kryukov 6,532
1 OriginalGriff 5,976
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,550


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2014
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