【问题标题】:NHibernate.MappingException: No persister forNHibernate.MappingException:没有持久性
【发布时间】:2014-01-22 23:56:44
【问题描述】:

我正在使用 nHibernate 开始我的冒险,但我遇到了一个问题。

我的代码: 模型/Project.cs

namespace entity1.Model
{
    public class Project
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

模型/Project.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="entity1.Model" assembly="entity1.Model">
  <class name="entity1.Model.Project, entity1.Model" lazy="false">
    <id name="id" column="prj_id"></id>
    <property name="Name" column="prj_name" />
    <property name="Description" column="prj_description" />
  </class>
</hibernate-mapping>

Web.config

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Server=(local);initial catalog=todo;Integrated Security=True</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
      <mapping assembly="entity1.Model"/>
    </session-factory>
  </hibernate-configuration>

Test.aspx.cs

Project project = new Project();
// [...]
Configuration c = new Configuration();
c.AddAssembly(Assembly.GetCallingAssembly());

ISessionFactory factory = c.BuildSessionFactory();
using (ISession session = factory.OpenSession()) {
    using(ITransaction transaction = session.BeginTransaction()){
        session.Save(project);
        transaction.Commit();
}

还有例外: 没有持久化器:entity1.Model.Project

怎么了?

非常感谢大家的帮助。 对不起我的英语不好。不太好。

【问题讨论】:

    标签: c# nhibernate orm mapping


    【解决方案1】:

    你确定你的程序集叫做entity1.Model吗? 我认为这只是命名空间,程序集是entity1 对吗?

    如果您不确定,请查看项目的属性。

    然后在您的 web.config 中更改它

    <mapping assembly="entity1"/>
    

    和映射文件

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="entity1.Model" assembly="entity1">
    

    您可能错过了调用 c.Configure() 来加载 xml 配置。

    【讨论】:

    • 我在项目属性中检查了程序集。有组件:entity1。主命名空间:entity1。接下来我在映射文件和 web.config 中为 namespace="entity1.Model" 和 assembly="entity1" 设置命名空间和程序集,不幸的是它也是错误的。
    • 你可能会错过电话configuration.Configure()
    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2011-04-26
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多