.NET 常用ORM之Gentle.Net
.Net常用的就是微软的EF框架和Nhibernate,这两个框架用的都比较多就不做详细介绍了,今天我们来看看Gentle.Net,Gentle.Net是一个开源的优秀O/R Mapping的对象持久化框架。Gentle.Net在配置和使用上要远比NHibernate简单很多。
Gentle.Net可以说是比较容易上手,语法使用也相对简单,但Gentle.Net的使用要依赖一个东西,那就是代码生成器,因为这对于它来说,是最重要的一步了,这个代码生成使用起来也很方便,本文也会给大家来介绍这个代码生成器的使用。Gentle.Net的优点是配置和使用都比较方便,所有能很多程度的减低开发成本。
首先你要是使用Gentle.Net需要下载几个东西:
①. Gentle.Net文件包(其中包含dll文件、类文件生成器模板)
Gentle.Net-1.5.0 下载文件包介绍:
Build\
强名密钥文件,NDoc文档生成文件等
Configuration\
配置文件示例,App.config也修改为Web.config文件。
Contributions\
代码生成器的模板文件,装上代码生成器之后双击这些文件就可以使用。
Documentation\
Gentle.Net相关的说明文档。
Output\
Gentle.Net的生成dll文件。
Source\
Gentle.Net源代码。
②.MyGeneration或CodeSmith代码生成器
下载的文件度娘上有很多链接地址,在这里笔者就不放下载链接了。
下面说说具体使用:
1.新建一个工程,在下载的 Gentle.Net文件包找到所需的dll添加到项目中
2.建一个测试数据库(下文将以下表作为测试数据表)
3.根据下载文件包Gentle.NET 1.5.0\Configuration\App.config 配置web.config文件,如下:其中两处log4的配置的文件如果不需要可以不做添加,gentle配置节点下
<DefaultProvider name="SQLServer" connectionString="Data Source=127.0.0.1;Initial Catalog=Test;User ID=sa;Password=XXXXXXXX;" />数据库连接改成自己的即可,还有<Providers>节点中把所要使用的SQLServer配置打开,把其他的数据都注释掉,配置文件就OK了。
4.代码生成器生成gentle所需要的实体类,一下将以codesmith做介绍:
(1)下载安装.....;
(2)可以找到gentle文件的中的生成器模板 F:\XXX\GentleNet\Gentle-1.5.0\Gentle.NET 1.5.0\Contributions\CodeSmith\GentleBusinessObject.cst,安装好了生成器工具就是可以双击打开模板,最主要需要改的就是SourceTable,其他都可以不做改动;
4.2.1>选择数据源
4.2.2>
4.2.3>
4.2.4>这里要说的是MSSql需要选择SqlSchemaProvider里面可没有SQLServer的选项
4.2.5>到了这个页面就很熟悉了,就是配置并选择数据源信息
4.2.6>选择完之后,选择Generate
4.2.7>生成的类代码大概长这样
/// <summary>
/// TODO add description of Userinfo here.
/// </summary>
[Serializable]
[TableName("UserInfo")]
public class Userinfo : Persistent
{
#region Private member data
[TableColumn("ID", NotNull=true), PrimaryKey(AutoGenerated=true) ]
protected int id;
[TableColumn("Name", NotNull=true) ]
protected string name = String.Empty;
[TableColumn("Age", NotNull=true) ]
protected int age;
[TableColumn("Sex", NotNull=true) ]
protected int sex;
[TableColumn("Men", NotNull=true) ]
protected int men;
[TableColumn("Remark") ]
protected string remark = String.Empty;
#endregion #region Constructors
/// <summary>
/// Create Userinfo from existing/full data set (used by the data layer).
/// </summary>
public Userinfo( int id, string name, int age, int sex, int men, string remark ) : base( true )
{
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.men = men;
this.remark = remark; }
/// <summary>
/// Select an existing Userinfo given its unique identifier
/// </summary>
static public Userinfo Retrieve( int id )
{
Key key = new Key( typeof(Userinfo), true, "id", id );
return Broker.RetrieveInstance( typeof(Userinfo), key ) as Userinfo;
}
#endregion #region Public Properties
///<summary>
/// ID accesses the ID column of the UserInfo table.
/// This is the Identity column for the Table. It can only be read.
///</summary>
public int ID
{
get{ return id; }
} ///<summary>
/// Name accesses the Name column of the UserInfo table.
///</summary>
public string Name
{
get{ return name; }
set{ name = value; }
} ///<summary>
/// Age accesses the Age column of the UserInfo table.
///</summary>
public int Age
{
get{ return age; }
set{ age = value; }
} ///<summary>
/// Sex accesses the Sex column of the UserInfo table.
///</summary>
public int Sex
{
get{ return sex; }
set{ sex = value; }
} ///<summary>
/// Men accesses the Men column of the UserInfo table.
///</summary>
public int Men
{
get{ return men; }
set{ men = value; }
} ///<summary>
/// Remark accesses the Remark column of the UserInfo table.
///</summary>
public string Remark
{
get{ return remark; }
set{ remark = value; }
}
5.类文件生成完成,将类代码copy到项目中,就可以享用GentleNet的便利,记得项目文件得引用Gentle相关dll文件,否则实体会报错
6.代码简单调用测试
导入命名空间
using Gentle.Common;
using Gentle.Framework;
using Gentle.Provider.SQLServer;
using Model;
实际使用
public ActionResult Index()
{
//1.新增
//Userinfo modUser = new Userinfo(6, "Jojo", 22, 1, 1, "GentleNetTest");
//Gentle.Framework.Broker.Insert(modUser); // 2.复杂查询[支持t-sql]
string sql = "select * from UserInfo where name like '%himi%'";
int s = Gentle.Framework.Broker.Execute(sql).Rows.Count; return View();
}
看到测试数据的变好了嘛?本文只是对gentle的一个小试牛刀,当然这只是最基本的使用,还有更多更复杂的使用,具体的要根据实际业务来定,这里就不细说了。。。
.NET 常用ORM之Gentle.Net的更多相关文章
- ORM概述及常用ORM框架
一.ORM ORM(Object-relational mapping),即对象关系映射,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.也就是说,ORM是通过使用描述对象和数据库之间映 ...
- .NET 常用ORM之SubSonic
一.SubSonic简单介绍 SubSonic是一个类似Rails的开源.NET项目.你可以把它看作是一把瑞士军刀,它可以用来构建Website和通过ORM方式来访问数据.Rob Conery和Eri ...
- .NET 常用ORM之iBatis
ibatis 一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目,到后面发展的版本叫MyBatis但都是指的同一个东西.最初侧重 ...
- .Net 常用ORM框架对比:EF Core、FreeSql、SqlSuger
前言: 最近由于工作需要,需要选用一种ORM框架,也因此对EF Core.FreeSql.SqlSuger作简单对比.个人认为各有有优势,存在即合理,不然早就被淘汰了是吧,所以如何选择因人而议.因项目 ...
- .NET 常用ORM之NHibernate
NHibernate做.Net应该都不陌生,今天我们就算是温故下这个技术,概念性的东西就不说了,这次主要说本人在实际使用的遇到的问题,比较费解现在就当是记录下,避免以后再犯.本次主要使用的情况是1对N ...
- .NET 常用ORM之Nbear
NBear是一个基于.Net 2.0.C#2.0开放全部源代码的的软件开发框架类库.NBear的设计目标是尽最大努力减少开发人员的工作量,最大程度提升开发效率,同时兼顾性能及可伸缩性. 一.新建项目并 ...
- yii 常用orm
yii2 orwhere andwhere的复杂写法:https://www.codercto.com/a/6513.html $files = XXXX::find() ->andWhere( ...
- django ORM
http://www.cnblogs.com/alex3714/articles/5512568.html 常用ORM操作 一.示例Models from django.db import model ...
- 轻量级ORM框架 QX_Frame.Bantina(二、框架使用方式介绍)
轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...
随机推荐
- shell中expect介绍
expect介绍 借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员 ...
- MSDN、OEM、VOL、RETAIL密钥区别
本文就介绍一下Windows的密钥的一些使用要点及注意事项,涉及到最常用的MSDN密钥.OEM密钥.VOL密钥和零售密钥激活问题,希望对大家有所帮助. 一.MSDN密钥 MSDN密钥是付费用户提前获得 ...
- vue 手指长按触发事件
按钮 <span class="btn" @touchstart="gtouchstart()" @touchmove="gtouchmove( ...
- swiper默认第二个且居中
var mySwiper = new Swiper ('.swiper-bottom', { spaceBetween: 25, freeMode: true, initialSlide :1,//默 ...
- DL中train\dev\test集
转自:https://blog.csdn.net/l8947943/article/details/80328721 training set:训练集是用来训练模型的.遵循训练集大,开发,测试集小的特 ...
- 【Java】-NO.16.EBook.4.Java.1.010-【疯狂Java讲义第3版 李刚】- 异常
1.0.0 Summary Tittle:[Java]-NO.16.EBook.4.Java.1.010-[疯狂Java讲义第3版 李刚]- 异常 Style:EBook Series:Java Si ...
- CentOS下用yum命令安装jdk【转】
一.使用yum命令安装 1.查看是否已安装JDK,卸载 [root@192 ~]# yum list installed |grep java java-1.8.0-openjdk.x86_64 ...
- PHP DDOS的UDP攻击,TCP攻击,和CC攻击的核心代码
网络安全向,请勿用作非法用途 CC攻击模块: <?phpecho “状态 : 正常运行中…..<br>”;echo “================================ ...
- Adobe Acrobat 9 Pro序列号
其实只删除c:\Program Files\Common Files\Adobe\Adobe PCD\cache目录下的cache.db文件也是可以的,然后重新打开Adobe ,输入序列号1118-4 ...
- unicode gbk utf-8的差异
GB2312(1980年)定义,包含6763个汉字,682个字符 GBK1.0 定义了21003个汉字,21886个字符 ASCII->GB2312->GBK 编码方式向后兼容,即同一个字 ...