使用NHibernate从外键获取MySQL表中的数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用NHibernate从外键获取MySQL表中的数据相关的知识,希望对你有一定的参考价值。
我有一个带有树表的mysql数据库:Author,PublishingHouse和Book。最后一个有两个外键 - AuthorId和PublishingId。我为每个表和NHibernate的映射文件创建了类。下一步是从Book表中获取数据并将其传递给我的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表中获取数据,它们非常简单,但Book表对我来说并不那么明显。我在绘图时错了吗?或者别的地方?
答案
书类
<class name="Book" table="Book" dynamic-update="true" >
有一个作者和一个出版社。我们需要多对一的映射
所以,而不是这个:
<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" />
期望Book表具有引用列Author_ID和PublishingHouse_ID
文件:
5.1.11. many-to-one
使用多对一元素声明与另一个持久类的普通关联。关系模型是多对一关联。 (它实际上只是一个对象参考。)...
另外,不要错过这个:
以上是关于使用NHibernate从外键获取MySQL表中的数据的主要内容,如果未能解决你的问题,请参考以下文章