using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TaidouDatebase
{
class NHibernateHelper//这个帮助类是用来得到session,运用sessionFactory来得到session,使用session来操作数据库,所以 NHibernateHelper可以认为是数据库的一个操作类
{
private static ISessionFactory sessionFactory = null;//单例模式 sessionFactory可以取得session,利用session可以去操作数据库。
private static void InitializeSessionFanctory()
{
/* 错误提示:“FluentNHibernate.Cfg.FluentConfigurationException”类型的未经处理的异常在 FluentNHibernate.dll 中发生
其他信息: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
解决方法: 管理NuGet程序包 下载mysql.date.dll
*/
//sessionFactory的初始化
sessionFactory = Fluently.Configure().Database(MySQLConfiguration.Standard.ConnectionString
(db => db.Server("localhost").Database("taidou").Username("root").Password("169169"))).Mappings(x => x.FluentMappings.AddFromAssemblyOf<NHibernateHelper>()).BuildSessionFactory();
/* MySQLConfiguration configuration = MySQLConfiguration.Standard.ConnectionString
(db => db.Server("localhost").Database("taidou").Username("root").Password("169169"));
sessionFactory =
Fluently.Configure()
.Database(configuration)
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<NHibernateHelper>())
.BuildSessionFactory();*/
}
private static ISessionFactory SessionFactory//用来得到SessionFactory
{
get
{
if (sessionFactory == null)
InitializeSessionFanctory();
return sessionFactory;
}
}
public static ISession OpenSession()//从 SessionFactory得到Session的方法
{
return SessionFactory.OpenSession();
}
}
}
TaidouDatebase-NHibernateHelper
最新推荐文章于 2019-12-26 15:32:28 发布