使用 NHibernate 使用外键从 MySQL table 获取数据
Get data from MySQL table with foreign keys using NHibernate
我有一个 MySQL 数据库,其中包含 table 树:作者、出版社和书籍。最后一个有两个外键 - AuthorId 和 PublishingId。
我为每个 table 制作了 类 并为 NHibernate 制作了一个映射文件。
下一点是从 Book table 中获取数据并将其传递到我的 MVC 项目的视图中。
我不能这样做,因为在我的 BookController 中我遇到了这样的异常:
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches);
这是我现在的代码:
型号
public class Author
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
public class PublishingHouse
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string City { get; set; }
}
public class Book
{
public virtual int Id { get; set; }
public virtual string UDK { get; set; }
public virtual string BBK { get; set; }
public virtual string AuthorsSign { get; set; }
public virtual string ISBNrus { get; set; }
public virtual string ISBNeng { get; set; }
public virtual Author Author { get; set; }
public virtual string Name { get; set; }
public virtual PublishingHouse PublishingHouse { get; set; }
public virtual int Year { get; set; }
public virtual int Pages { get; set; }
public virtual int Circulation { get; set; }
public virtual bool IsRead { get; set; }
public virtual int Rating { get; set; }
public virtual string Annotation { get; set; }
}
NHibernate 映射
<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" />
<set name="Author" table="Author">
<key column ="Id" />
<one-to-many class="Author"/>
</set>
<property name="Name" />
<set name="PublishingHouse" table="PublishingHouse">
<key column ="Id" />
<one-to-many class="PublishingHouse"/>
</set>
<property name="Year" />
<property name="Pages" />
<property name="Circulation" />
<property name="IsRead" />
<property name="Rating" />
<property name="Annotation" />
</class>
</hibernate-mapping>
BookController 我得到一个异常:
public ActionResult Index()
{
using (ISession connectionDB = NHibertnateSession.OpenSession())
{
var books = connectionDB.Query<Book>().ToList();
return View(books);
}
}
我可以很容易地从 Author 和 Publishing table 获取数据,它们很简单,但是 Book table 对我来说不是那么明显。我在映射中错了吗?或者其他地方?
本书class
<class name="Book" table="Book" dynamic-update="true" >
有一位作者和一家出版社。我们需要many-to-one映射
所以,而不是这个:
<set name="Author" table="Author">
<key column ="Id" />
<one-to-many class="Author"/>
</set>
<set name="PublishingHouse" table="PublishingHouse">
<key column ="Id" />
<one-to-many class="PublishingHouse"/>
</set>
我们需要:
<many-to-one name="Author" column="Author_ID" />
<many-to-one name="PublishingHouse" column="PublishingHouse_ID" />
希望图书 table 具有参考列 Author_ID 和 PublishingHouse_ID
文档:
5.1.11. many-to-one
An ordinary association to another persistent class is declared using
a many-to-one element. The relational model is a many-to-one
association. (It's really just an object reference.) ...
另外,不要错过这个:
我有一个 MySQL 数据库,其中包含 table 树:作者、出版社和书籍。最后一个有两个外键 - AuthorId 和 PublishingId。
我为每个 table 制作了 类 并为 NHibernate 制作了一个映射文件。
下一点是从 Book table 中获取数据并将其传递到我的 MVC 项目的视图中。
我不能这样做,因为在我的 BookController 中我遇到了这样的异常:
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches);
这是我现在的代码:
型号
public class Author
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
public class PublishingHouse
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string City { get; set; }
}
public class Book
{
public virtual int Id { get; set; }
public virtual string UDK { get; set; }
public virtual string BBK { get; set; }
public virtual string AuthorsSign { get; set; }
public virtual string ISBNrus { get; set; }
public virtual string ISBNeng { get; set; }
public virtual Author Author { get; set; }
public virtual string Name { get; set; }
public virtual PublishingHouse PublishingHouse { get; set; }
public virtual int Year { get; set; }
public virtual int Pages { get; set; }
public virtual int Circulation { get; set; }
public virtual bool IsRead { get; set; }
public virtual int Rating { get; set; }
public virtual string Annotation { get; set; }
}
NHibernate 映射
<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" />
<set name="Author" table="Author">
<key column ="Id" />
<one-to-many class="Author"/>
</set>
<property name="Name" />
<set name="PublishingHouse" table="PublishingHouse">
<key column ="Id" />
<one-to-many class="PublishingHouse"/>
</set>
<property name="Year" />
<property name="Pages" />
<property name="Circulation" />
<property name="IsRead" />
<property name="Rating" />
<property name="Annotation" />
</class>
</hibernate-mapping>
BookController 我得到一个异常:
public ActionResult Index()
{
using (ISession connectionDB = NHibertnateSession.OpenSession())
{
var books = connectionDB.Query<Book>().ToList();
return View(books);
}
}
我可以很容易地从 Author 和 Publishing table 获取数据,它们很简单,但是 Book table 对我来说不是那么明显。我在映射中错了吗?或者其他地方?
本书class
<class name="Book" table="Book" dynamic-update="true" >
有一位作者和一家出版社。我们需要many-to-one映射
所以,而不是这个:
<set name="Author" table="Author">
<key column ="Id" />
<one-to-many class="Author"/>
</set>
<set name="PublishingHouse" table="PublishingHouse">
<key column ="Id" />
<one-to-many class="PublishingHouse"/>
</set>
我们需要:
<many-to-one name="Author" column="Author_ID" />
<many-to-one name="PublishingHouse" column="PublishingHouse_ID" />
希望图书 table 具有参考列 Author_ID 和 PublishingHouse_ID
文档:
5.1.11. many-to-one
An ordinary association to another persistent class is declared using a many-to-one element. The relational model is a many-to-one association. (It's really just an object reference.) ...
另外,不要错过这个: