C# NHibernate.MappingException:没有持久化器用于:nhibernateeexperiment.Player-我可以';我找不到解决办法
我尝试使用NHibernate ORM创建一个项目,有一刻,当我认为我已经完成了所有工作时,它给了我一个NHibernate.MappingException:No persister for异常 我读到问题可能是我没有在配置文件中添加程序集,但我这样做了,而且它也没有被修复 如果有人有时间,请帮我解决这个问题 下面是我调用的代码,用于添加一个新的Player对象C# NHibernate.MappingException:没有持久化器用于:nhibernateeexperiment.Player-我可以';我找不到解决办法,c#,.net,nhibernate,C#,.net,Nhibernate,我尝试使用NHibernate ORM创建一个项目,有一刻,当我认为我已经完成了所有工作时,它给了我一个NHibernate.MappingException:No persister for异常 我读到问题可能是我没有在配置文件中添加程序集,但我这样做了,而且它也没有被修复 如果有人有时间,请帮我解决这个问题 下面是我调用的代码,用于添加一个新的Player对象 private void btnInsert_Click(object sender, EventArgs e) {
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();
}
private void btn插入\u单击(对象发送方,事件参数e)
{
玩家数据=新玩家();
SetPlayerInfo(playerData);
使用(ISession session=SessionFactory.OpenSession)
{
使用(ITransaction transaction=session.BeginTransaction())
{
尝试
{
session.Save(playerData);//它吐出来了
Commit();
GetPlayerInfo();
}
捕获(例外情况除外)
{
transaction.Rollback();
掷骰子;
}
}
}
}
私有void GetPlayerInfo()
{
使用(ISession session=SessionFactory.OpenSession)
{
IQuery=session.CreateQuery(“来自播放器”);
IList pInfos=query.List();
dgvDisplay.DataSource=pInfos;
}
}
私有无效设置playerInfo(播放器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>
<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>
下面是App.config的代码
<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>
<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>
NHibernate.Connection.DriverConnectionProvider
NHibernate.Driver.SqlClientDriver
服务器=GRITCAN;数据库=testDB;综合安全=SSPI;
NHibernate.dialogue.mssql2008dialogue
真的
NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu
这是堆栈跟踪
在nhibernateeexperiment.Form1.btn插入\单击e:\projects\tests\NHibernate\nhibernateeexperiment\nhibernateeexperiment\Form1.cs中的(对象发送者,事件参数e):第72行
在System.Windows.Forms.Control.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上
在System.Windows.Forms.Control.WmMouseUp(Message&m、MouseButtons按钮、Int32单击)
位于System.Windows.Forms.Control.WndProc(Message&m)
位于System.Windows.Forms.ButtonBase.WndProc(Message&m)
在System.Windows.Forms.Button.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)的System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason、ApplicationContext context)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
在E:\projects\tests\NHibernateExperiment\NHibernateExperiment\nhibernateeexperiment\Program.cs中的nhibernateeexperiment.Program.Main()处:System.AppDomain.的第16行。\u下一个ecuteAssembly(运行时程序集,字符串[]参数)
位于Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()的System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
我在项目中添加了以下两个参考
NHibernate.dll和NHibernate.ByteCode.LinFu.dll
非常感谢您的帮助强>
谢谢你们,孩子们。 .hbm.xml文件的构建操作是内容。
正如您建议的那样,我将其更改为嵌入式资源,一切正常:)是否将映射文件设置为嵌入式资源?检查xml映射是否标记为嵌入式资源。另外,我建议您使用Fluent nHibernate库-这是一种不用编写大量xml映射的自由,只需.net类即可工作的解决方案: Form1.cs 使用NHibernate; 使用NHibernate.Cfg; 使用制度; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用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
名称空间NHibernateTutorialPart1
{
公共部分类Form1:Form
{
私有配置;
私人ISessionFactory mySessionFactory;
私人会话;
公共表格1()
{
初始化复合物