与数据库的关系 [英] Which is the relation to database

查看:69
本文介绍了与数据库的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将在此处编写用于从数据库访问数据的代码.我的意思是直接链接到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 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)



现在回答您的问题,如果您已经在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆