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 'SPRING.ROOT': ERROR THROWN BY A DEPENDENCY OF OBJECT 'SYSTEM.DATA.SQLITE'
从网上下载的源码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 ...
随机推荐
- 【设计模式】—— 状态模式State
前言:[模式总览]——————————by xingoo 模式意图 允许一个对象在内部改变它的状态,并根据不同的状态有不同的操作行为. 例如,水在固体.液体.气体是三种状态,但是展现在我们面前的确实不 ...
- Java之Set的使用场景
2.Set使用场景 API介绍: java.util.Set接口和java.util.List接口一样,同样继承自Collection接口, 它与Collection接口中的方法基本一致,并没有对Co ...
- bzoj 3531 [Sdoi2014]旅行 (树剖+线段树 动态开点)
3531: [Sdoi2014]旅行 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2984 Solved: 1312[Submit][Status ...
- HGOI20180815 (NOIP 提高组模拟赛 day2)
Day 2 rank 11 100+35+30=165 本题是一道数论题,求ax+by=c的正整数对(x,y) x>=0并且y>=0 先说下gcd: 求a,b公约数gcd(a,b) 如gc ...
- Django中简单添加HTML、css、js等文件(非正规添加,适合小白)
Django中简单添加HTML.css.js等文件 首先申明下自己的环境, python版本3.65(亲测3.7版本有毒,没解决掉!) Django版本1.11.15(版本比较成熟,也可以用最新的版本 ...
- gulpfile.js不断更新中...
Gulp压缩合并js/css文件,压缩图片,以及热更新教程 var gulp = require('gulp');var concat = require('gulp-concat');//- 多个文 ...
- MySQL初始化以及客户端工具的使用
MySQL初始化以及客户端工具的使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是关系型数据库 关系型数据库通常是把所有的数据都组织成二维关系.之所以称为关系型数据库是 ...
- 异步消息处理机制Handler
Android 中的异步消息处理主要由四个部分组成,Message.Handler.MessageQueue 和Looper. 1. Message Message 是在线程之间传递的消息,它可以在内 ...
- 蓝桥杯 算法训练 单词接龙 _DFS_搜索 字符串比较
单词接龙 问题描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相 ...
- [整理]Assembly中的DLL提取
当机器上安装一些程序后,Assembly中的DLL会变得越来越丰富. 拿个常见问题来说明. 安装ReportViewer后其中会出现以下DLL. Microsoft.ReportViewer.Proc ...