NHibernate.MappingException:没有持久性

时间:2014-01-05 15:34:04

标签: c# nhibernate orm mapping

我开始使用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

有什么问题?

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

1 个答案:

答案 0 :(得分: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配置。

相关问题