NHibernate MappingException,没有持久性:NHiberDeneme.Sinifi

时间:2012-07-19 14:23:07

标签: nhibernate

的app.config

<?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="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Data Source=localhost;Initial Catalog=hiberdb;Integrated Security=True;</property>
      <property name="default_schema">hiberdb.dbo</property>
      <mapping assembly="NHiberDeneme"/>
    </session-factory>
  </hibernate-configuration>
</configuration> 

Program.cs的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;

namespace NHiberDeneme
{
    class Program
    {

        static void Main(string[] args)
        {
            Configuration cfg = new Configuration();
            cfg.AddAssembly("NHibernate"); // projenin assembly Adi
            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            Sinifi sinif = new Sinifi();
            sinif.Id = 1;
            sinif.SinifAdi = "sinif adi";
            session.Save(sinif);
            session.Flush();
            session.Clear();
            transaction.Commit();
            session.Close();
        }
    }
}

Sinifi.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Mapping;

namespace NHiberDeneme  
{
    class Sinifi 
    {
        private int _id;
        private string _sinifadi;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string SinifAdi
        {
            get { return _sinifadi; }
            set { _sinifadi = value; }
        }
    }
}

Sinifi.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="NHiberDeneme.Sinifi, NHiberDeneme.Program, NHiberDeneme" table="Sinifi" lazy="false">
    <id name="Id" column="Id" type="int">
      <generator class="assigned" />
    </id>
    <property name="SinifAdi" column="SinifAdi" type="String" length="10"/>


    <bag name="SinifiId" table="SinifiId" inverse="true">
      <key column="Id" />
      <one-to-one class="NHiberDeneme.Sinifi, NHiberDeneme.Program, NHiberDeneme" />
    </bag>

  </class>
</hibernate-mapping>

我在这里的sql关系http://i.stack.imgur.com/jOT5M.jpg

给我“MappingException,No persister for:NHiberDeneme.Sinifi”错误。我尝试了很多方法,我也嵌入了资源。

1 个答案:

答案 0 :(得分:0)

有了这个错误,NHibernate会告诉你类映射有问题。 NHibernate要么找不到类映射,要么无法正确解析它。

通过查看它,映射文件中的类名似乎是错误的。

来自NHibernate关于class mapping的文档:

  

name :持久化类的完全限定的.NET类名(或   接口),包括其程序集名称。

在您的代码中,您有类名:NHiberDeneme.Sinifi, NHiberDeneme.Program, NHiberDeneme。这不是有效的.NET assembly qualified class name。如果您的程序集名称为NHiberDeneme,则应该具有以下内容:

<class name="NHiberDeneme.Sinifi, NHiberDeneme" ... />

...如果程序集名称为NHiberDeneme.Program,则映射应如下所示:

<class name="NHiberDeneme.Sinifi, NHiberDeneme.Program" ... />