2012-06-27 37 views
2

我尝试使用NHibernate ORM制作一个项目,并且在我认为所有完成的时候,它给了我一个NHibernate.MappingException:没有留存为例外 我看这个问题可能是因为我没有加入配置文件中的程序集,但我这样做,而且它不是固定的......NHibernate.MappingException:没有persister:NHibernateExperiment.Player - 我找不到解决方案

如果有人有一个小时,请帮我解决问题。

这里是我打电话的代码添加一个新的Player对象

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(); 
     } 

这里映射Player.hbm.xml代码

<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> 

这里是应用。 config代码

<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> 

这里是栈跟踪

在NHibernateExperiment.Form1.btnInsert_Click(对象发件人,EventArgs e)如E:\项目\测试\ NHibernate的\ NHibernateExperiment \ NHibernateExperiment \ Form1.cs中:在系统管线72 。 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(消息&米) 在System.Windows.Forms.Button.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM) 在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.Applicat ion.ThreadContext.RunMessageLoopInner(的Int32原因,ApplicationContext的上下文) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32原因,ApplicationContext的上下文) 在System.Windows.Forms.Application.Run(表格的MainForm) 在NHibernateExperiment .Program.Main()在E:\ projects \ tests \ NHibernate \ NHibernateExperiment \ NHibernateExperiment \ Program.cs中:第16行在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args) at System.AppDomain.ExecuteAssembly(String (System.ThreadingHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上的assemblyFile,Evidence assemblySecurity,String [] args) ,对象状态,布尔ean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state) at System.Threading.ThreadHelper。的ThreadStart()

我已经添加了以下2个引用该项目 NHibernate.dllNHibernate.ByteCode.LinFu.dll

非常感谢您的帮助!


谢谢你BOYS。 构建动作对于.hbm.xml文件是内容。 正如你建议我,我把它改成嵌入式资源和:)

+1

请务必改变*生成操作*为'.hbm.xml'文件*嵌入的资源*。 –

+0

谢谢。 ** .hbm.xml **文件的** Build Action **是** Content **。我将其更改为** Embedded Resource **,并且所有工作正常:) – meorfi

回答

2

检查xml映射是否标记为嵌入资源。我还reccomend您使用功能NHibernate库 - 这是从写XML映射的数额巨大,仅.NET clasess一个自由

+0

您不需要Fluent来替换xml。您也可以使用代码映射,从3.2版开始使用NH功能。 –

+0

很高兴知道它 - 非常酷的功能。对不起,我没有按照最新的变化,仍然使用1.X版本 –

3

你做的映射文件嵌入资源一切工作正常?

+0

谢谢。 ** .hbm.xml **文件的** Build Action **是** Content **。我将其更改为**嵌入式资源**,并且所有工作正常:) – meorfi

+1

这还不够 - 请检查此文件Build Action proferty。它应该是嵌入式资源 –

0

工作解决方案:使用NHibernate

Form1.cs的 ; 使用NHibernate.Cfg;使用系统的 ; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;使用System.Linq的 ; using System.Text; using System.Threading.Tasks;使用System.Windows.Forms的 ;

namespace NHibernateTutorialPart1 
{ 
    public partial class Form1 : Form 
    { 

     private Configuration myConfiguration; 
     private ISessionFactory mySessionFactory; 
     private ISession mySession; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      myConfiguration = new Configuration(); 
      myConfiguration.Configure("hibernate_mysql.cfg.xml"); 
      mySessionFactory = myConfiguration.BuildSessionFactory(); 
      mySession = mySessionFactory.OpenSession(); 

      using (mySession.BeginTransaction()) 
      { 
       Contact lbContact=new Contact{FirstName="Nisha", LastName="Shrestha",ID=0}; 
       mySession.Save(lbContact); 
       mySession.Transaction.Commit(); 
      } 
     } 
    } 
} 

**hibernate_mysql.cfg.xml** 

<?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="dialect">NHibernate.Dialect.MySQLDialect</property> 
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property> 
    <property name="connection.connection_string"> 
     User Id=root; 
     Server=localhost; 
     Password=Password1; 
     Database=nhibernatecontacts; 
    </property> 

    <!--This is good for Debugging but not otherwise--> 
    <property name="show_sql">true</property> 

    <!--<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>--> 
    <mapping assembly="NHibernateTutorialPart1"/> 
    </session-factory> 
</hibernate-configuration> 


**contact.hbm.xml** 

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="NHibernateTutorialPart1" 
        namespace="NHibernateTutorialPart1"> 
    <class name="Contact" table="contact"> 
    <id name="ID" column="ID"> 
     <generator class="identity" /> 
    </id> 
    <property name="FirstName" /> 
    <property name="LastName" /> 
    </class> 
</hibernate-mapping> 

**Contact class** 


namespace NHibernateTutorialPart1 
{ 
    public class Contact 
    { 

     public virtual int ID { get; set; } 
     public virtual string FirstName { get; set; } 
     public virtual string LastName { get; set; } 

    } 
} 


**App.config** 

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/> 
    </configSections> 
</configuration> 

You need to add reference for lesi.collection and NHibernate 
You need to do embeded resource for contact.hbm.xml and hibernate_mysql.cfg.xml 

我用在我创建了一个新的模式和新的表ID,姓氏和名字MySQL工作台。

对于没有留存异常的解决方案是我忘了在contact.hbm.xml给表名

相关问题