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/

最新文章

  1. 配置React Native环境
  2. GUID相关知识。。。。转载
  3. php 获取中文字符拼音首字母
  4. 【linux】修改文件所属用户和组
  5. Mininet实验 基于Mininet实现BGP路径挟持攻击实验
  6. Part 100 Func delegate in c#
  7. LXD 2.0 系列(二):安装与配置
  8. 利用flashback query 恢复表数据
  9. AIDL与stub
  10. SQL 查找存储过程及视图与自带函数
  11. 批量建立EXCHANGE邮件帐号建立三部曲
  12. ExecuteNonQuery返回负数
  13. AppSettings
  14. IHttpModule与IHttpHandler的区别整理
  15. SQL SERVER 2008R2sp1配置Database Mail –用SQL 数据库发邮件
  16. 在visual studio的工程项目应用中打开console控制窗口
  17. AMD规范学习笔记
  18. vue 中的translation操作----动态值
  19. 【webpack系列】从零搭建 webpack4+react 脚手架(一)
  20. MySql 中文写入数据库乱码及Incorrect string value: &#39;\xF0\x9F...&#39; for column &#39;XXX&#39; at row 1解决

热门文章

  1. swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B
  2. BZOJ5467 PKUWC2018Slay the Spire(动态规划)
  3. bzoj1345 序列问题 (贪心)
  4. sadpairs
  5. token的理解
  6. HDFS之append数据到已存在文件中
  7. 说说SQL Server的数据类型
  8. Linux命令(七)Linux用户管理和修改文件权限
  9. .NET面试题系列(五)数据结构(Array、List、Queue、Stack)及线程安全问题
  10. (FFT)A+B Problem