C#中Nhibernate中如何传递两个连接字符串?我的应用程序有一个问题:我有两个数据库,我想用NHibernate访问它们,但是在配置文件中我只有一个数据库的连接字符串。那么如何将多个连接字符串传递给NHibernate呢?我通常在app.config中定义连接字符串:然后我使用nhibernate配置创建2个单独的(nhibernate)配置文件(如果您有2个不同的数据库)。我使用一个允许我创建会话的类:publicclassNHibernateSessionFactory{privateISessionFactorysessionFactory;私有只读字符串ConnectionString="";私有只读字符串nHibernateConfigFile="";publicNHibernateSessionFactory(StringconnectionString,stringnHConfigFile){this.ConnectionString=connectionString;this.nHibernateConfigFile=nHConfigFile;}publicISessionFactorySessionFactory{get{returnsessionFactory??(sessionFactory=CreateSessionFactory());}}privateISessionFactoryCreateSessionFactory(){配置cfg;cfg=newConfiguration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,this.nHibernateConfigFile));//Nhibernate在下面的这一行中搜索App.Config中的连接字符串。//cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName,System.Environment.MachineName);cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString,this.ConnectionString);#if调试配置文件.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics,"真");cfg.SetProperty(NHibernate.Cfg.Environment.ShowSql,"true");#endif返回(cfg.BuildSessionFactory());现在,我可以使用您自己的特定配置创建许多不同的会话工厂:varsessionFactory1=newNHibernateSessionFactory("connectionstring1","sql.nhibernate").SessionFactory;varsessionFactory2=newNHibernateSessionFactory("连接字符串2","ora.nhibernate").SessionFactory;您可以在此处找到更多信息对于每个数据库,您都需要一个自己的SessionFactory。如果在NH配置中省略ConnectionString,可以在构建SessionFactory时在Code中指定:以上是C#学习教程:HowtopasstwoconnectionstringsinNhibernateinC#?所有分享的内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——varsessionFactory1=newConfiguration().Configure().SetProperty("connection.connection_string","第一个连接字符串").BuildSessionFactory();varsessionFactory2=newConfiguration().Configure().SetProperty("connection.connection_string","第二个连接字符串").BuildSessionFactory();本文收集自网络,不代表立场,如有侵权请点右联系管理员删除。如需转载请注明出处:
