如何在MySQL服务器和Microsoft SQL Server 2008中使用NHibernate
如何配置NHibernate连接MySQL服务器和Microsoft SQL Server 2008? 我确实想将数据从一台服务器复制到另一台服务器。 我听说过NHibernate的共享。
我几个月前就已经挣扎了。 我的问题出在MS Sql Server和Oracle上。
我所做的是为nhibernate创建两个单独的配置文件:
sql.nhibernate.config
NHibernate.Connection.DriverConnectionProvider NHibernate.Driver.SqlClientDriver NHibernate.Dialect.MsSql2008Dialect web 100 120 3 true true 1, false 0, yes 'Y', no 'N' NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
ora.nhibernate.config
NHibernate.Connection.DriverConnectionProvider NHibernate.Driver.OracleDataClientDriver NHibernate.Dialect.Oracle10gDialect web 100 120 3 true true 1, false 0, yes 'Y', no 'N' NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
我使用这个简单的类来构建我的nhibernate SessionFactory:
public class NHibernateSessionFactory { private ISessionFactory sessionFactory; private readonly string ConnectionString = ""; private readonly string nHibernateConfigFile = ""; public NHibernateSessionFactory(String connectionString, string nHConfigFile) { this.ConnectionString = connectionString; this.nHibernateConfigFile = nHConfigFile; } public ISessionFactory SessionFactory { get { return sessionFactory ?? (sessionFactory = CreateSessionFactory()); } } private ISessionFactory CreateSessionFactory() { Configuration cfg; cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.nHibernateConfigFile)); // With this row below Nhibernate searches for the connection string inside the App.Config. // cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName); cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, this.ConnectionString); #if DEBUG cfg.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true"); cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql, "true"); #endif return (cfg.BuildSessionFactory()); } }
正如您所看到的,我将连接字符串(我更喜欢将其保存在我的应用程序配置文件中)和nhibernate配置文件的名称(不带路径)传递给我的NHibernateSessionFactory。
我个人使用DI容器(StructureMap),你可以实现一个非常酷的定义注册表类:
public class NhibernateRegistry : Registry { public NhibernateRegistry() { For() .Singleton() .Add(new NHibernateSessionFactory(, "ora.nhibernate.config").SessionFactory) .Named("OracleSF"); For() .HybridHttpOrThreadLocalScoped() .Add(o => o.GetInstance("OracleSF").OpenSession()) .Named("OracleSession"); For () .Singleton() .Add(new NHibernateSessionFactory(, "sql.nhibernate.config").SessionFactory) .Named("MsSqlSF"); For() .HybridHttpOrThreadLocalScoped() .Add(o => o.GetInstance("MsSqlSF").OpenSession()) .Named("MsSqlSession"); } }
您可以在其中使用命名实例。 我的服务层比使用StructureMap注册表类,您可以在其中定义构造函数:
this.For() .HybridHttpOrThreadLocalScoped() .Use() .Ctor("sessionMDII").Is(x => x.TheInstanceNamed("OracleSession")) .Ctor ("sessionSpedizioni").Is(x => x.TheInstanceNamed("MsSqlSession"));
对于您的服务实施:
public class OrdersService : IOrdersService { private readonly ISession SessionMDII; private readonly ISession SessionSpedizioni; public OrdersService(ISession sessionMDII, ISession sessionSpedizioni) { this.SessionMDII = sessionMDII; this.SessionSpedizioni = sessionSpedizioni; } ... }
目前尚不清楚您的用例是什么,但您只需要创建2个会话工厂。 一个将使用您的mySQL方言,连接字符串等…另一个将使用SQL Server等价物。
只是避免在映射中包含任何自定义SQL,这应该可以正常工作。