与数据库的关系 [英] Which is the relation to database
问题描述
我们将在此处编写用于从数据库访问数据的代码.我的意思是直接链接到mvc中的数据库,"Controller"或"MOdel"
谢谢,
Dileep
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
推荐答案
您应该详细学习MVC(我建议您读一本书),如果您来自事件驱动的编程世界,则需要一段时间才能熟悉了解MVC概念.以下是快速概述.
Model
-是数据库的包装,主要使用ORM(对象关系映射),即 LINQ或实体框架,您可以编写 Web服务或简单数据访问类使用Model
访问数据库.
View
-是应用程序对用户的外观,用户将看不到Controller
或Model
,用户将使用View
与您的应用程序进行交互.View
包含 HTML,Razor,Javascript/jQuery,UI元素等.
Controller
-是Model
和View
之间的中间人,它将从所得的View
动作向Model
发送命令.即,如果用户单击View
Controller
上的"保存"按钮,则会向Model
发送命令以执行"更新/插入"操作.
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 usingModel
.
View
- is how the application will look to user, user will not seeController
orModel
, user will interact with your application usingView
.View
contains HTML, Razor, Javascript/jQuery, UI Elements etc..
Controller
- is the middle man betweenModel
andView
, it will send command toModel
from the resultingView
actions. i.e. if user clicks "Save" button onView
Controller
will send command toModel
to execute "Update/Insert" action.
MVC Relationship
----------------
(user side)
View
|
|
Controller
|
|
Model
(database side)
现在回答您的问题,如果您已经在使用Entity Framework/LINQ并且Model
已经存在.那么一种快速的方法是使用Controller
中的LINQ表达式来调用数据库来操纵Model
.否则,您需要在Model
下指定数据库操作方法,然后在Controller
中调用这些方法.
因此Model
将直接链接到数据库,并且如果您不想编写那些传统的数据访问代码,请使用ORM(例如Entity Framework或LINQ).
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).
大多数人会建议使用存储库模式"将数据访问代码移出控制器.这是一个链接 [ ^ ]给出示例和参考.我已经使用这两种方法创建了项目,但是我认为拥有Repository类是一个干净的解决方案.
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.
您应该阅读以下文章以消除混淆:
架构指南:ASP.NET MVC框架+ N层+实体框架等等 [ ^ ]
使用ASP.NET MVC进行N层开发, WCF和LINQ [ ^ ]
此处 [
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
这篇关于与数据库的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!