NHibernate - 如果连接可以传递给会话构造函数,为什么需要连接来构建工厂?

时间:2009-11-02 19:25:28

标签: nhibernate db2

我是NHibernate的新手,并且正在尝试使用它连接到DB2。经过反复试验和研究,以及更多的反复试验和研究,我终于找到了一个我无法找到答案的错误。

错误消息显示:“用户必须提供ADO.NET连接 - NHibernate不会创建它。”

此错误不是很有帮助。这是否意味着我需要1)在我的配置文件中放置一个连接字符串或2)我是否需要将连接传递到工厂?如果1为真,这是强制性的吗?如果是,那么为什么我们能够将连接对象传递给我想要做的会话?

如果2)为真,我如何将连接对象传入工厂?

提前谢谢你。以下是我的代码片段,可能会有所帮助:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    </configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="dialect">NHibernate.Dialect.DB2400Dialect</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    </session-factory>
</hibernate-configuration>  
</configuration>

try
{               
    Configuration cfg = new Configuration();  

    cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(persistentClass));
    SessionFactory = cfg.Configure().BuildSessionFactory(); //HERE I GET THE EXCEPTION
}
catch (Exception ex)
{
    throw new Exception("NHibernate initialization failed", ex);
}

2 个答案:

答案 0 :(得分:1)

你需要会话工厂进行配置,比如连接,你使用什么样的映射,你使用什么样的缓存,什么样的sql方言等等。不仅仅是连接,所以传递连接作为构造函数参数可能会给出你另一个无益的例外。您必须在配置中设置连接。

答案 1 :(得分:0)

如果查看documentation,您应该能够看到如何以编程方式创建sessionfactory。或者,您可以使用流畅的Nhibernate来避免该配置文件。