Using NHibernate with SQLite
The most convenient method to add NHibernate and SQLite for C# project is using NuGet. You can check that in [1]. But I prefer a more free way to do it with NAnt. Let's do it.
Step 1, download all packages from official websites.
Step 2, set up the directory structure
For SQLite files:
For NHibernate files:
Step 3, create your domain entity class.
// Product.cs
namespace NHExample.Domain
{
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual int Price { get; set; } }
}
Step 4, create the mapping file Product.hbm.xml.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHExample"
namespace="NHExample.Domain"> <class name="Product" table="products">
<id name="Id">
<generator class="guid" />
</id> <property name="Name" />
<property name="Category" />
<property name="Price" />
</class> </hibernate-mapping>
Step 5, create NHibernate configuration file hibernate.cfg.xml.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">Data Source=nhibernate.db;Version=3</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Step 6, create a driver for it
// NHExample.cs using System;
using System.Linq;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl; namespace NHExample
{
class NHExample
{
static void Main(string[] args)
{
// Initialize NHibernate
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Domain.Product).Assembly); // Get ourselves an NHibernate Session
var sessions = cfg.BuildSessionFactory();
var sess = sessions.OpenSession(); // Create the database schema
new SchemaExport(cfg).Create(true, true); // Create a Product...
var product = new Domain.Product
{
Name = "Some C# Book",
Price = 500,
Category = "Books"
}; // And save it to the database
sess.Save(product);
sess.Flush(); // Note that we do not use the table name specified
// in the mapping, but the class name, which is a nice
// abstraction that comes with NHibernate
IQuery q = sess.CreateQuery("FROM Product");
var list = q.List<Domain.Product>(); // List all the entries' names
list.ToList().ForEach( p => Console.WriteLine( p.Name ));
}
}
}
Step 7, create a NAnt script.
<?xml version="1.0"?>
<project name="SQLite with NHibernate" default="run">
<property name="debug" value="true" />
<property name="outdir" value="bin" />
<property name="libdir" value="../lib" />
<property name="sqlite_libdir" value="../../sqlite/lib" />
<target name="clean" description="remove all generated files">
<delete dir="${outdir}" />
</target>
<target name="build" description="compiles the source code">
<mkdir dir="${outdir}" />
<csc debug="${debug}" output="${outdir}/NHExample.exe" target="exe">
<references basedir="${libdir}">
<include name="NHibernate.dll" />
</references>
<sources>
<include name="Domain/Product.cs" />
<include name="NHExample.cs" />
</sources>
<resources dynamicprefix="true" prefix="NHExample.Domain">
<include name="Mappings/*.hbm.xml" />
</resources>
</csc>
<copy todir="${outdir}">
<fileset basedir="${libdir}">
<include name="NHibernate.dll" />
<include name="NHibernate.xml" />
<include name="Iesi.Collections.dll" />
<include name="Iesi.Collections.xml" />
</fileset>
</copy>
<copy todir="${outdir}">
<fileset basedir="etc">
<include name="hibernate.cfg.xml" />
</fileset>
</copy>
<copy todir="${outdir}">
<fileset basedir="${sqlite_libdir}">
<include name="System.Data.SQLite.dll" />
<include name="System.Data.SQLite.xml" />
</fileset>
</copy>
<copy todir="${outdir}/x64">
<fileset basedir="${sqlite_libdir}/x64">
<include name="SQLite.Interop.dll" />
</fileset>
</copy>
<copy todir="${outdir}/x86">
<fileset basedir="${sqlite_libdir}/x86">
<include name="SQLite.Interop.dll" />
</fileset>
</copy>
</target>
<target name="run" depends="build">
<exec program="${outdir}/NHExample.exe" workingdir="${outdir}"/>
</target>
</project>
Step 8, build and run it
Links:
NHibernate: http://nhforge.org/
SQLite.NET: http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki
References:
[1] http://coding-journal.com/setting-up-nhibernate-with-sqlite-using-visual-studio-2010-and-nuget/
Using NHibernate with SQLite的更多相关文章
- Could not create the driver from NHibernate.Driver.SQLite20Driver
使用NHibernate连接Sqlite语句,版本为.net3.5. 升级.net 4.0出现异常,提示”Could not create the driver from NHibernate.Dri ...
- USING NHIBERNATE WITH MySQL
In previous USING NHIBERNATE WITH SQLITE, we connect SQLITE with ORM framework NHibernate. One of th ...
- lync项目总结
概述 9月份,由于公司人事变动,摆在自己面前也有两条路可选择,一是选择lync,二是选择sharepoint,由于之前,部门老大已经让我看了大概一个月的有关lync方面的资料(文档,代码,项目实施等) ...
- [Nhibernate]sqlite数据库基本使用
目录 写在前面 操作步骤 总结 写在前面 昨天有朋友问我在nhibernate中如何使用sqlite数据库,当时实在忙的不可开交,下周要去山西出差,实在没空,按我的说法使用sqlite跟使用sqlse ...
- Fluent NHibernate and Mysql,SQLite,PostgreSQL
http://codeofrob.com/entries/sqlite-csharp-and-nhibernate.html https://code.google.com/archive/p/csh ...
- 启用SQLite的Data Provider 运行WECOMPANYSITE时遇到ERROR CREATING CONTEXT &#39;SPRING.ROOT&#39;: ERROR THROWN BY A DEPENDENCY OF OBJECT &#39;SYSTEM.DATA.SQLITE&#39;
从网上下载的源码WeCompanySite,运行时报错 Error creating context 'spring.root': Error thrown by a dependency of ob ...
- 耗时两月,NHibernate系列出炉
写在前面 这篇总结本来是昨天要写的,可昨天大学班长来视察工作,多喝了点,回来就倒头就睡了,也就把这篇总结的文章拖到了今天. nhibernate系列从开始着手写,到现在前后耗费大概两个月的时间,通过总 ...
- 【翻译】Fluent NHibernate介绍和入门指南
英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...
- 【翻译】首个基于NHibernate的应用程序
首个基于NHibernate的应用程序 Your first NHibernate based application 英文原文地址:http://www.nhforge.org/wikis/how ...
随机推荐
- Javascript 面向对象编程(一):封装
Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类). 那么,如果 ...
- sql编程(四)触发器
触发器:一种特殊的存储过程,和制定的表相关,而对于一个表进行的数据操作时自动的触发执行类似winform 事件创建语法:create trigger 触发器的名称on 表名 for insert | ...
- 【Jenkins】Linux搭建Jenkins平台
为了配合上一篇的ant+jenkins做持续集成,需要在linux环境下搭建一个jenkins平台.网上有很多安装的例子,我主要记录一下自己遇到的问题,真真的是特别惆怅的,每次我遇到的问题都格外多. ...
- sprintf mfc
sprintf(d,"char is %d",wparam);wparam以%d形式存入d中 并不是输出到屏幕上!
- Laravel OAuth2 (三) ---使用 services 和 facades
前言 既然要判断用户是否存在,然后创建用户,那么就要实现几个功能函数.为了方便调用,于是我尝试着写了第一个service 和 facade . 创建 Facade class Social exten ...
- Git客户端(Windows系统)的使用(Putty)(转)
本文环境: 操作系统:Windows XP SP3 Git客户端:TortoiseGit-1.8.14.0-32bit 一.安装Git客户端 全部安装均采用默认! 1. 安装支撑软件 msysgit: ...
- dubbo安装
dubbo 管控台可以对注册到 zookeeper 注册中心的服务或服务消费者进行管理,分享牛系列,分享牛专栏,分享牛.但管控台是否正常对 Dubbo 服务没有影响,管控台也不需要高可用,因此可以单节 ...
- .Net Core 在Linux服务器下部署程序--(3). 部署.net core 后端程序
确认第二步中的软件已安装完成 lrzsz文件上传下载软件 zip与unzip压缩包软件 net core 相关软件 确认上述软件安装完成之后,开始部署程序 创建部署文件夹 我的习惯是在usr文件夹下新 ...
- Python面向对象中的类和对象
类和对象 目标 类和对象的概念 类和对象的关系 类的设计 01. 类和对象的概念 类 和 对象 是 面向对象编程的 两个 核心概念 1.1 类 类 是对一群具有 相同 特征 或者 行为 的事物的一个统 ...
- python的内置函数time
time 模块 1 >>> import time 2 >>> time.time() 3 1491064723.808669 4 >>> # t ...