我只是想开始学习 NHibernate,在使用极其简单的 POCO 进行测试时,我已经遇到了问题。我得到了 No Persister 异常,这是我的代码的样子:
Account
桌子:
create table Account
(
AccountID int primary key identity(1,1),
AccountName varchar(10),
CreateDate datetime
)
go
Account
班级:
public class Account
{
public virtual int AccountID { get; set; }
public virtual string AccountName { get; set; }
public virtual DateTime CreateDate { get; set; }
public Account()
{
AccountID = 0;
AccountName = "";
CreateDate = new DateTime();
}
}
映射文件,Account.hbm.xml
(是的,它嵌入到程序集中):
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernateTesting" assembly="NHibernateTesting">
<class name="Account" table="Account">
<id name="AccountID">
<generator class="native"/>
</id>
<property name="AccountName" />
<property name="CreateDate" />
</class>
</hibernate-mapping>
配置文件中的配置部分:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">My connection string</property>
<mapping assembly="NHibernateTesting" />
</session-factory>
</hibernate-configuration>
最后,进行调用的代码:
using (var session = NHibernateHelper.GetASession())
{
using (var tran = session.BeginTransaction())
{
var newAccount = new Account();
newAccount.AccountName = "some name";
session.Update(newAccount); // Exception thrown here.
tran.Commit();
}
}
谁能看到我做错了什么或者为什么我会得到这个异常,我在网上读到这个问题与映射有关,但我看不出这个例子有什么问题。