C# – NHibernate.MappingException: No persister for: NHibernateExperiment.Player – i can’t find the solution

cnetnhibernate

I try to make a project with the NHibernate ORM, and at one moment when i thought that all I finished , it gives me an NHibernate.MappingException: No persister for exception
I read that the problem could be that i didn't added the assembly in config file, but I did this, and also it is not fixed…

If someone has a litle time, help me please to fix the problem.

here is the code I call to add a new Player object

private void btnInsert_Click(object sender, EventArgs e)
        {
            Player playerData = new Player();
            SetPlayerInfo(playerData);

            using (ISession session = SessionFactory.OpenSession)
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Save(playerData); // here it spits
                        transaction.Commit();
                        GetPlayerInfo();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
            }

        }

private void GetPlayerInfo()
        {
            using (ISession session = SessionFactory.OpenSession)
            {
                IQuery query = session.CreateQuery("FROM Player");
                IList<Player> pInfos = query.List<Player>();
                dgvDisplay.DataSource = pInfos;
            }
        }

private void SetPlayerInfo(Player playerData)
        {
            playerData.PlayerName = tbxName.Text;
            playerData.PlayerAge = Convert.ToInt32(tbxAge.Text);
            playerData.DOJ = Convert.ToDateTime(dtpDOJ.Text);
            playerData.BelongsTo = cmbBelongsTo.SelectedItem.ToString();
        }

here is the mapping Player.hbm.xml code

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="NHibernateExperiment.Player, NHibernateExperiment" lazy="true">
    <id name="PlayerId">
      <generator class="native"/>
    </id>
    <property name="PlayerName" column ="PlayerName"/>
    <property name="PlayerAge" column ="PlayerAge"/>
    <property name="DOJ" column="DOJ"/>
    <property name="BelongsTo" column="BelongsTo"/>
  </class>

</hibernate-mapping>

here is the App.config code

    <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="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Server=GRITCAN;database=testDB;Integrated Security=SSPI;</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="show_sql">true</property>          
      <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>          
    </session-factory>
  </hibernate-configuration>
</configuration>

here is the StackTrace

at NHibernateExperiment.Form1.btnInsert_Click(Object sender, EventArgs e) in E:\projects\tests\NHibernate\NHibernateExperiment\NHibernateExperiment\Form1.cs:line 72
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at NHibernateExperiment.Program.Main() in E:\projects\tests\NHibernate\NHibernateExperiment\NHibernateExperiment\Program.cs:line 16 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I've added the following 2 references to the project
NHibernate.dll and NHibernate.ByteCode.LinFu.dll

Thanks a lot for your help!


Thank You BOYS.
The Build Action for .hbm.xml file was Content.
As you suggested me I changed it to Embedded Resource and all works fine:)

Best Answer

Check if xml mapping is marked as embedded resource. Also I'd reccomend you to use Fluent nHibernate library - this is a freedom from writing huge amount of xml mappings, only .net clasess