ASP.NET MVC学习---(一)ORM框架,EF实体数据模型简介

现如今

对象关系映射(ORM)框架

被大量的使用于企业级应用的开发

为什么要使用ORM?

ADO.NET操作数据库不好吗?


我们可以仔细想想

当我们使用ADO.NET操作数据库的时候

我们需要先获取连接字符串

接着根据连接字符串创建一个SqlConnection对象来打开与数据库的连接

紧接着还要创建SqlCommand对象来执行数据库命令

根据不同的命令还要创建相应的不同的对象来进行操作

比如SqlDataAdapter和DataSet等

另外这次操作完成之后

还要注意关闭数据库的连接通道,释放资源等问题


或许当我们操作ADO.NET习惯了之后并不觉得哪里麻烦

但是相信使用过ORM的同学在真正开发的时候很少在直接用ADO.NET来操作数据库了

真的真的有这么好用嘛?

骗你又没糖吃

举一个很简单的例子(可能不是很恰当,将就着吧...)

比如现在你想吃一道菜

需要经过自己去准备食材,点火拿锅炒菜等一系列的动作

但是现在给你提供了一个大厨

不不,大厨不好

给你一个可爱温柔,厨艺又好的漂亮老婆

这个可以有吧?

只要告诉老婆你想吃什么菜

漂亮老婆就会帮你做出来

而不需要你去经历做菜的过程

在这期间你还可以去做其他的事情(在此可自由发挥想象力...)

同样是吃到想吃的菜

你是选择费些精力自己去做

还是让老婆做?

这不是废话嘛!

而且老婆做的菜肯定比你自己做的好吃!

人家是有练过的!

ORM框架就相当于是你的老婆

...

同样是如此,ORM老婆操作数据库的性能和效率往往是比自己写的数据库操作要好


这就是ORM老婆框架所做的事情

她将数据库中的表和程序中的类用一种映射关系关联起来

做到了对数据库层的屏蔽

在之前,程序员需要耗费大量的时间、精力去编写具体的数据库访问的SQL语句

还要十分小心其中大量重复的代码是否有疏漏,并不能集中精力于业务逻辑开发

但是老婆的出现帮助程序员解决了这些问题

留给程序员的不在是繁琐的数据库操作

而且他们相当熟悉的类和对象

这从一方面大大降低了代码量,也使程序员更加专注于业务逻辑的实现

正如我们所知

数据库表与表之间的关系有可能十分复杂

1对1、1对多、多对1、多对多、级联等

在操作数据库时,程序员必须小心谨慎的注意这些关系

而这往往是十分痛苦的过程

ORM框架通过程序中的类和数据库中的表

建立起了一种关系映射

程序员通过操作熟悉的类和对象即可实现对数据库的操作

ORM框架会自动帮我们维护这些复杂的关系

这就做到了对数据库层的屏蔽

使得程序员可以方便,快捷的进行数据库操作


至于之前说到的性能问题

这就不得不讲到ORM框架一个十分重要和牛逼的技术

延迟加载

ORM框架将根据具体数据库操作需要,会自动延迟向后台数据库发送SQL请求

从而大大降低与数据库的交互次数,提高数据库吞吐率提高运行效率

此外

ORM也可以根据实际情况,将数据库访问操作合成,尽量减少不必要的数据库操作请求


这个老婆是不是很贴心?

方方面面都给你想到了你还不要?

不管你要不要反正我是要了

用她又不用钱!是吧


接下来介绍一下EF

Entity Framework是微软以 ADO.NET 为基础所发展出来的对象关系对应解决方案

ORM框架的中的一种

(众多老婆中的一个)

在早期

人们在.NET平台下经常使用的ORM框架是NHibernate

这是一个Java平台的Hibernate移植过来的ORM框架

其强大的功能和性能深受程序员的喜爱

这可是正房夫人呀

早早的就虏获了众多程序员的心

但是现在人们的中心已经渐渐的转移到了二房

没错

就是EF

注意,她不是小三...

原因很简单

看过狗血电视剧的同学应该都知道

二房一般都是比正房夫人漂亮的!

不然人家大地主为毛要娶那么多个

不就是因为一个比一个漂亮

一个比一个好用嘛!(这里好像有点用词不当...)

EF就是一个比NHibernate漂亮,好用的老婆

因为她能和.NET平台完美的结合

而且提供可视化的关系对象映射模型

前面提到过

ORM框架根据程序的类和对象与数据库的表建立起了一种映射关系

这个映射关系通过xml文档保存在程序内部(比如配置文件)

在NHibernate中

程序员需要编写很繁杂的xml代码来实现这种映射关系

而这个过程同样也是十分痛苦的

常常因为一点点小的配置问题导致无法访问数据库

这就是正房夫人的不对了吧

你让本大人受罪

再娶一个气死你

而在EF中

提供了一种edmx文件

它本质上还是一个xml文档

但是它可以为程序员提供一个可视化的界面图形

人们可以通过简单的鼠标或者键盘操作来完成关系的映射

看吧,是不是比那个当正房的漂亮又好用多了


ORM和EF的简单介绍到此为止

与此同时本菜鸟的MVC之旅也拉开序幕


欲知后事如何,且听下回分解

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对象关系映射架构(DBFramework)及代码生成器源码 一、使用对象关系映射组件Kenly.DBFramework.dll不用编写任何SQL或者存储过程即可实现下列功能: 1、数据表、视图和存储过程与对象之间的转换。 2、数据表、视图的自定义条件查询。 3、数据表、视图的分页查询。 4、根据ID、主键或自定义条件对数据表进行增、删、改操作。 5、实现一对一、一对多、多对一和多对多的关系映射。 6、支持单个对象和多个对象之间的事务控制。 7、支持查询结果排序。 8、支持查询表达式生成。 9、支持延迟加载。 二、代码生成器 1、根据指定的数据库连接,自动生成数据表、视图和存储过程对应的对象代码(C#代码)。 2、自动生成相关的工程文件,生成完整的业务层项目文件。 3、可以帮助生成自定义查询方法。 4、支持SQLServer2000、SQLServer2005和Oracle代码生成插件,支持插件扩展。 提供 1、对象关系映射组件: Kenly.DBFramework.dll 2、代码生成器源码:CodeHelper。 3、代码生成器插件源码(支持SQLServer2000、SQLServer2005和Oracle):Plugin。 4、使用手册:DBFramework.Manual V4.5.3.pdf。 主要API: public abstract class ViewGateway where T: new() { // Methods static ViewGateway(); protected ViewGateway(); protected static int Count(); protected static int Count(string condition); protected static List CountGroup(string groupBy); protected static List CountGroup(string condition, string groupBy); public static List CreateInstances(int count); protected static List Distinct(string columnName); protected static List Distinct(string columnName, string condition); protected static List Distinct(string columnName, string condition, bool ignoreNull); protected static bool Exists(string condition); public bool ExistsById(); public bool ExistsByPK(); protected static List Find(PagingArg pagingArg, params string[] propertyNames); protected static List Find(string condition, params string[] propertyNames); protected static List Find(PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static List Find(string condition, PagingArg pagingArg, params string[] propertyNames); protected static List Find(string condition, PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static List FindAll(params string[] propertyNames); protected static void FindTop(T firstOne); protected static List FindTop(int topCount, params string[] propertyNames); protected static void FindTop(string condition, T firstOne); protected static List FindTop(string condition, int topCount, params string[] propertyNames); protected static void InitializeGateway(GatewayConfig config); protected static void InitializeGateway(DatabaseType dbType, string connectionString); protected void InitializeInstance(T entity); protected static object Max(string columnName); protected static object Max(string columnName, string condition); protected static List MaxGroup(string columnName, string groupBy); protected static List MaxGroup(string columnName, string condition, string groupBy); protected static object Min(string columnName); protected static object Min(string columnName, string condition); protected static List MinGroup(string columnName, string groupBy); protected static List MinGroup(string columnName, string condition, string groupBy); protected static DataTable Query(PagingArg pagingArg, params string[] propertyNames); protected static DataTable Query(string condition, params string[] propertyNames); protected static DataTable Query(PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static DataTable Query(string condition, PagingArg pagingArg, params string[] propertyNames); protected static DataTable Query(string condition, PagingArg pagingArg, bool sqlServer2000, params string[] propertyNames); protected static DataTable QueryAll(params string[] propertyNames); protected static DataTable QueryTop(int topCount, params string[] propertyNames); protected static DataTable QueryTop(string condition, int topCount, params string[] propertyNames); public ArrayList RetrieveAssociations(); public ArrayList RetrieveAssociations(Type elementType); public ArrayList RetrieveAssociations(params Type[] elementTypes); public void RetrieveById(); public void RetrieveByPK(); protected internal virtual void RetrieveBySql(string sql); public void ShadowCopyTo(object targetEntity); public void ShadowCopyTo(T targetEntity); protected static double Sum(string columnName); protected static double Sum(string columnName, string condition); protected static List SumGroup(string columnName, string groupBy); protected static List SumGroup(string columnName, string condition, string groupBy); public static string ToColumnName(string propertyName); public static T ToObject(DataRow adaptedRow); public static List ToObjects(DataTable adaptedTable); public static List ToObjects(DataRow[] adaptedRows); public static string ToPropertyName(string columnName); public static DataTable ToTable(IList entities); public static DataTable ToTable(params T[] entities); public static DataTable ToTable(bool isAdapted, params T[] entities); public static DataTable ToTable(bool isAdapted, IList entities); // Properties protected internal static string CommandText { get; } protected internal static GatewayConfig Config { get; } internal T Entity { get; set; } public object EntityId { get; } public object PrimaryKey { get; } protected static Order SortedOrder { get; set; } } public abstract class TableGateway : ViewGateway where T: TableGateway, new() { // Methods static TableGateway(); protected TableGateway(); public int AddNew(); public static int AddNew(IList entities); public static int AddNew(T entity); public int AddNew(bool returnIdentity); public static int AddNew(T entity, bool returnIdentity); public static int AddNew(IList entities, out Transaction transaction); public static int AddNew(IList entities, bool returnIdentity); public static int AddNew(T entity, out Transaction transaction); public static int AddNew(IList entities, bool returnIdentity, out Transaction transaction); public static int AddNew(T entity, bool returnIdentity, out Transaction transaction); public IDbTransaction BeginTransaction(); public IDbTransaction BeginTransaction(IsolationLevel isolationLevel); public IDbTransaction BeginTransaction(double activeTime); public IDbTransaction BeginTransaction(IsolationLevel isolationLevel, double activeTime); public bool Commit(); protected static int Delete(string condition); protected static int Delete(string condition, out Transaction transaction); public int DeleteById(); public static int DeleteById(T entity); public static int DeleteById(IList entities); public static int DeleteById(IList entities, out Transaction transaction); public static int DeleteById(T entity, out Transaction transaction); public int DeleteByPK(); public static int DeleteByPK(T entity); public static int DeleteByPK(IList entities); public static int DeleteByPK(IList entities, out Transaction transaction); public static int DeleteByPK(T entity, out Transaction transaction); protected void OnPropertyChanged(MethodBase propertyMethod); protected void OnPropertyChanged(string propertyName); protected internal override void RetrieveBySql(string sql); public bool Rollback(); protected static int Update(T entity, string condition); protected static int Update(T entity, string condition, out Transaction transaction); public int UpdateById(); public static int UpdateById(IList entities); public static int UpdateById(T entity); public static int UpdateById(T entity, out Transaction transaction); public static int UpdateById(IList entities, out Transaction transaction); public int UpdateByPK(); public static int UpdateByPK(IList entities); public static int UpdateByPK(T entity); public static int UpdateByPK(IList entities, out Transaction transaction); public static int UpdateByPK(T entity, out Transaction transaction); // Properties public bool Changed { get; } public List ChangedPropertyNames { get; } } public class StoredProcedure : IStoredProcedure { public bool BeginTransaction(); public void CloseReader(IDataReader reader); public bool Commit(); public DataSet ExecuteDataSet(); public DataTable ExecuteDataTable(); public DataTable ExecuteDataTable(); public List ExecuteEntity(); public int ExecuteNonQuery(); public IDataReader ExecuteReader(); public object ExecuteScalar(); protected static void InitializeGateway(GatewayConfig config); protected static void InitializeGateway(DatabaseType dbType, string connectionString); protected void InitializeInstance(T entity); public bool Rollback(); } public class AggregateEntity where T: AggregateEntity, new() { public static List Execute(); protected static List Execute(string condition); public static DataTable ExecuteDataTable(); protected static DataTable ExecuteDataTable(string condition); protected static void InitializeGateway(GatewayConfig config); protected static void InitializeGateway(DatabaseType dbType, string connectionString); // Properties protected static Order SortedOrder { get; set; } } public static class EntityMapper { // Methods public static void AdaptToDatabase(DataTable adaptedTable); public static void AdaptToEntity(DataTable rawTable); public static void CopyToEntities(IList entities, IList adaptedRows); public static void CopyToEntities(IList entities, DataTable adaptedTable); public static void CopyToEntity(T entity, DataRow row); public static List CreateEntities(int count); public static DataTable CreateTable(); public static DataTable CreateTable(bool isAdapted); public static string ToColumnName(string propertyName); public static List ToEntities(IList adaptedRows); public static List ToEntities(DataTable table); public static T ToEntity(DataRow adaptedRow); public static string ToPropertyName(string columnName); public static DataTable ToTable(IList entities); public static DataTable ToTable(params T[] entities); public static DataTable ToTable(bool isAdapted, params T[] entities); public static DataTable ToTable(bool isAdapted, IList entities); } public static class EntityUtility { // Methods public static List Inherit(IList entities); public static T Inherit(object entity); public static T Inherit(Tbase baseEntity, TransformAction method); public static List Inherit(IList baseEntities, TransformAction method); public static void ShadowCopy(IList targetEntities, IList sourceEntities); public static void ShadowCopy(object targetEntity, object sourceEntity); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值