You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NHibernate配置报错:session-factory包含无效子元素add

NHibernate配置错误修复:session-factory中的无效元素

你的错误根源是在NHibernate的节点中使用了元素——这是EF等其他ORM的配置写法,NHibernate的配置规范里,只接受property、mapping、class-cache等官方指定的子元素,不识别标签。

修改后的配置XML

把原来的标签替换成NHibernate标准的标签,用来配置数据库连接字符串:

<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <!-- 替换原来的<add>为NHibernate标准的连接字符串配置 -->
        <property name="connection.connection_string">Data Source=(localdb)\mssqllocaldb;Initial Catalog=Database_name;Integrated Security=True;MultipleActiveResultSets=True</property>
        <property name="show_sql">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    </session-factory>
</hibernate-configuration>

补充说明

  • NHibernate通过connection.connection_string这个property指定数据库连接字符串,不需要providerName属性(你已经通过connection.driver_class指定了SqlClientDriver)。
  • 你的C#初始化代码没有问题,仅需修正XML配置即可解决报错。

内容的提问来源于stack exchange,提问作者Blues

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 22:04:56