.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的更多相关文章

  1. ORM概述及常用ORM框架

    一.ORM ORM(Object-relational mapping),即对象关系映射,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.也就是说,ORM是通过使用描述对象和数据库之间映 ...

  2. .NET 常用ORM之SubSonic

    一.SubSonic简单介绍 SubSonic是一个类似Rails的开源.NET项目.你可以把它看作是一把瑞士军刀,它可以用来构建Website和通过ORM方式来访问数据.Rob Conery和Eri ...

  3. .NET 常用ORM之iBatis

    ibatis 一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目,到后面发展的版本叫MyBatis但都是指的同一个东西.最初侧重 ...

  4. .Net 常用ORM框架对比:EF Core、FreeSql、SqlSuger

    前言: 最近由于工作需要,需要选用一种ORM框架,也因此对EF Core.FreeSql.SqlSuger作简单对比.个人认为各有有优势,存在即合理,不然早就被淘汰了是吧,所以如何选择因人而议.因项目 ...

  5. .NET 常用ORM之NHibernate

    NHibernate做.Net应该都不陌生,今天我们就算是温故下这个技术,概念性的东西就不说了,这次主要说本人在实际使用的遇到的问题,比较费解现在就当是记录下,避免以后再犯.本次主要使用的情况是1对N ...

  6. .NET 常用ORM之Nbear

    NBear是一个基于.Net 2.0.C#2.0开放全部源代码的的软件开发框架类库.NBear的设计目标是尽最大努力减少开发人员的工作量,最大程度提升开发效率,同时兼顾性能及可伸缩性. 一.新建项目并 ...

  7. yii 常用orm

    yii2 orwhere andwhere的复杂写法:https://www.codercto.com/a/6513.html $files = XXXX::find() ->andWhere( ...

  8. django ORM

    http://www.cnblogs.com/alex3714/articles/5512568.html 常用ORM操作 一.示例Models from django.db import model ...

  9. 轻量级ORM框架 QX_Frame.Bantina(二、框架使用方式介绍)

    轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...

随机推荐

  1. Linux查看某个端口使用情况并kill

    Linux查看某个端口使用情况并kill 例如查看8083端口的状态: netstat -apn | grep 8083 tcp 0 0 192.168.2.17:8083 0.0.0.0:* LIS ...

  2. confd动态生成配置文件

    下载安装confd $ mkdir -p $GOPATH/src/github.com/kelseyhightower $ git clone https://github.com/kelseyhig ...

  3. 【LeetCode每天一题】Multiply Strings(字符串乘法)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  4. 【LeetCode每天一题】Two Sum(两数之和)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. 【Redis】持久化

    Redis提供了为持久化提供了两种方法:第一种是快照:他可以将存在某一时刻的所有数据都写入硬盘里面.第二种是只追加文件(AOF):它会在执行命令时,将被执行的写命令复制到硬盘里面. Redis支持持久 ...

  6. [LeetCode] 581. Shortest Unsorted Continuous Subarray_Easy tag: Sort, Stack

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  7. [LeetCode] 824. Goat Latin_Easy

    A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...

  8. malloc调用后经历了什么?

    进程生成虚拟地址空间,有堆地址,由于是虚拟地址,所以没有做内存碎片化处理,只是在虚拟内存不够的时候调用brk,进行堆大小的调整,然后申请到虚拟内存是页,同MMU映射到物理地址,然后并不是每个页都预先加 ...

  9. electron 前端开发桌面应用

    electron是由Github开发,是一个用Html.css.JavaScript来构建桌面应用程序的开源库,可以打包为Mac.Windows.Linux系统下的应用. 快速开始 接下来,让代码来发 ...

  10. Facebook的bigpipe

    参考文档:英文版:http://www.cubrid.org/blog/dev-platform/faster-web-page-loading-with-facebook-bigpipe/ 搜索技术 ...