题外话:
虽然微软推出了EF4,但是目前支持EF4的虚拟空间很少,即使有支持的空间,价格也非常昂贵,所以使用NH3.1是比较好的选择,并且NH3.1与EF4一样支持Linq和Lambda表达式。如果对EF4有兴趣的,可以看我之前的一篇博文“Asp.net MVC 2.0 + Unity 2.0(IoC) + EF4.0 实例:RoRoWoBlog 开源项目框架代码”
需要的DLL库文件:
1、从官方下载NHibernate-3.1.0.GA-bin.zip,需要用到压缩包中的DLL文件如下:
NHibernate-3.1.0.GA-bin\Required_Bins\Iesi.Collections.dll
NHibernate-3.1.0.GA-bin\Required_Bins\NHibernate.dll
NHibernate-3.1.0.GA-bin\Required_For_LazyLoading\LinFu\NHibernate.ByteCode.LinFu.dll
NHibernate-3.1.0.GA-bin\Required_For_LazyLoading\LinFu\LinFu.DynamicProxy.dll
2、下载ConfORM1.0.1.4_NH3.1GA.zip,需要用到压缩包中的DLL文件如下:
ConfORM1.0.1.4_NH3.1GA\ConfOrm.dll
创建实体类:
using System.Text;
using System;
namespace DiPiPi.Infrastructure.Hibernate.Entity {
public class BlogTag {
public BlogTag() { }
public virtual int Tag_ID { get; set; }
public virtual string TagName { get; set; }
public virtual string ArticleIDs { get; set; }
}
}
增删改操作代码:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Dialect;
using NHibernate.Cfg;
using NHibernate.Cfg.Loquacious;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Driver;
using NHibernate.Tool.hbm2ddl;
using NHibernate.ByteCode.LinFu;
using ConfOrm;
using ConfOrm.NH;
using DiPiPi.Infrastructure.Hibernate.Entity;
namespace DiPiPi.Infrastructure.Hibernate
{
public static class DBHelper
{
private const string _ConnectionString =
@"Data Source=localhost;Initial Catalog=DiPiPiDB;Integrated Security=True;Pooling=False";
public static Configuration GetConfiguration()
{
var configure = new Configuration();
configure.SessionFactoryName("DiPiPiDB");
//Proxy扩展方法用于配置NHibernate延迟加载的字节码提供程序
configure.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>());
configure.DataBaseIntegration(db =>
{
db.Dialect<MsSql2005Dialect>();
db.Driver<SqlClientDriver>();
db.ConnectionString = _ConnectionString;
});
return configure;
}
public static HbmMapping GetMapping()
{
var orm = new ObjectRelationalMapper();
orm.TablePerClass<BlogTag>();
var mapper = new Mapper(orm);
return mapper.CompileMappingFor(new[] { typeof(BlogTag) });
}
public static void Test()
{
//配置NHibernate
var conf = GetConfiguration();
//在Configuration中添加HbmMapping
conf.AddDeserializedMapping(GetMapping(), "BlogTag");
//配置数据库架构元数据
SchemaMetadataUpdater.QuoteTableAndColumns(conf);
//创建数据库架构
new SchemaExport(conf).Create(false, true);
//建立SessionFactory
var factory = conf.BuildSessionFactory();
//打开Session做持久化数据
using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
var blogTag = new BlogTag { TagName = "我的测试" };
s.Save(blogTag);
tx.Commit();
}
}
//查询、排序
using (var s = factory.OpenSession())
{
var query = s.QueryOver<BlogTag>()
.Where(p => p.TagName == "我的测试")
.OrderBy(p => p.Tag_ID).Asc
.List();
}
//打开Session做删除数据
using (var s = factory.OpenSession())
{
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from BlogTag").ExecuteUpdate();
tx.Commit();
}
}
//删除数据库架构
new SchemaExport(conf).Drop(false, true);
}
}
}
另外值得一提的是,new SchemaExport(conf).Create(false, true); 这里是先写实体代码再创建的数据库哦,呵呵 ^_^,大家一直追求的 Code First 哈。