zoukankan      html  css  js  c++  java
  • Fluent NHibernate使用小结:(1)通用配置文件创建方法

    Fluent NHibernate 提供object To XML的支持,让我们可以直接在代码中编写NHibernateCfg配置文件。

    配置文件编写过程,总共分为4步:

    1, Fluently.Configure()   生成Cfg对象。

    2,.DataBase(x=>xxxxx) 生成数据库对象。

    3,.Mapping(x=>xxxx)    匹配Hbm描述。

    4,.BuildingSessionFactory 创建状态工厂。

    我这里对应Mysql配置文件提取出一个通用泛型SessionFactory工厂。

    private static object lockobject = new object();

    private Dictionary<string,ISessionFactory> m_SessionFactoryDictionary = new Dictionary<string,ISessionFactory>();

            
    public ISessionFactory CreateSessionFactory<T>(string connectStringName)
            {
                
    if (!m_SessionFactoryDictionary.ContainsKey(connectStringName))
                {
                    
    lock (lockobject)
                    {
                        
    if (!m_SessionFactoryDictionary.ContainsKey(connectStringName))
                        {

                            m_SessionFactoryDictionary[connectStringName] 
    = Fluently.Configure()
                                //配置数据库
                                .Database(
                                    FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString(
                                     x 
    => x.FromAppSetting(connectStringName)))//connectStringName是AppSetting中的链接字符串

                                .Mappings(x 
    => x.FluentMappings.AddFromAssemblyOf<T>())

                                .BuildSessionFactory();
                        }
                    }

                    
    return m_SessionFactoryDictionary[connectStringName];
                }

                
    return m_SessionFactoryDictionary[connectStringName];
            }

    类似Oracle,SqlServer也可以这样建立SessionFactory

     运行时候Mysql会要求加载对应的NHibernate.MysqlDriver.

    1, Mysql支持。

    下载Mysql驱动。

    http://mysql.spd.co.il/Downloads/Connector-Net/mysql-connector-net-6.2.4.zip

    安装后,用VS的Reference引用Mysql.Data.dll并且设置属性为Copy Local:True

    支持Windows 64bit。设置工程Platform targetAny CPU (原来是X86)

     

  • 相关阅读:
    mysql同步之otter/canal环境搭建完整详细版
    Linux安装aria2
    mysql多源复制(多主一从)配置
    分布式调度框架TBSchedule使用方法
    hbase shell插入根据条件查询数据
    hive内部表&外部表介绍
    Canal( 增量数据订阅与消费 )的理解及应用
    tidb入门
    ES命令
    java8新特性
  • 原文地址:https://www.cnblogs.com/GeeWu/p/fn_Configure.html
Copyright © 2011-2022 走看看