我正在寻找多对一NHibernate架构的CRUD操作示例。我发现只有基本的例子,他们没有帮助我。我尝试在数据库中插入一个新对象并获取NHibernate.Exceptions.GenericADOException
插入操作的代码:
public bool CreateBook(Book book)
{
try
{
using (ISession connectionDB = NHibertnateSession.OpenSession())
{
using (ITransaction transaction = connectionDB.BeginTransaction())
{
connectionDB.Save(book);
transaction.Commit();
}
}
return true;
}
catch
{
return false;
}
}
我想我需要做这样的事情才能让它发挥作用:
这是一个奇怪的解决方案,不是吗? 在这种情况下,哪种插入数据的方法是什么?
我的模型类(Book,Author,PublishingHouse)是here
这是我的hbm.xml
文件
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"
assembly="InFolio_HomeLib" namespace="InFolio_HomeLib.Models">
<class name="Author" table="Author" dynamic-update="true" >
<cache usage="read-write"/>
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="FirstName" />
<property name="LastName" />
</class>
<class name="PublishingHouse" table="PublishingHouse" dynamic-update="true">
<cache usage="read-write"/>
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="Name" />
<property name="City" />
</class>
<class name="Book" table="Book" dynamic-update="true" >
<cache usage="read-write"/>
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="UDK" />
<property name="BBK" />
<property name="AuthorsSign" />
<property name="ISBNrus" />
<property name="ISBNeng" />
<many-to-one name="Author" column="AuthorId" />
<property name="Name" />
<many-to-one name="PublishingHouse" column="PublishingId" />
<property name="Year" />
<property name="Pages" />
<property name="Circulation" />
<property name="IsRead" />
<property name="Rating" />
<property name="Annotation" />
</class>
P.S。如果您有其他CRUD操作的示例/参考,那就太酷了。