2015-04-07 45 views
0

剛開始在我的ASP.NET MVC應用程序中使用NHibernate。例如,我創建了兩個表格:書籍和章節。NHibernate多個表。 ASP.NET MVC

CREATE TABLE [dbo].[Books] (
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [Title] NVARCHAR (50) NOT NULL, 
    [Author] NVARCHAR (50) NOT NULL,  
); 

CREATE TABLE [dbo].[Chapters] (
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [Title]  NVARCHAR (MAX) NOT NULL, 
    [Notes]  TEXT   NULL, 
    [ChapterIndex] INT   NULL, 
    [BookId]  INT   NULL,  
    CONSTRAINT [FK_Chapters_ToTable] FOREIGN KEY (BookId) REFERENCES Books(Id) 
); 

NHibernate的配置文件:

<?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="connection.driver_class"> 
      NHibernate.Driver.SqlClientDriver 
     </property> 
     <property name="connection.connection_string"> 
      Data Source= LocalDB)\v11.0;AttachDbFilename="c:\users\anton\documents\visual studio 2013\Projects\Books\Books\App_Data\BooksDB.mdf";Integrated Security=True 
     </property> 
     <property name="dialect"> 
      NHibernate.Dialect.MsSql2012Dialect 
     </property> 
     </session-factory> 
    </hibernate-configuration> 

Book.hmb.xls映射文件

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="Books" namespace="Books.Models"> 

    <class name="Book" table="Books" dynamic-update="true"> 
    <cache usage="read-write"/> 
    <id name="Id" column="Id" type="int"> 
     <generator class="native" /> 
    </id> 
    <property name="Title" not-null="true" /> 
    <property name="Author" not-null="true"/> 
    <list name="Chapters" cascade="all-delete-orphan"> 
     <key column="BookId"/> 
     <index column="ChapterIndex"/> 
     <one-to-many class="Chapter"/> 
    </list> 
    </class> 

    <class name="Chapter" table="Chapters" dynamic-update="true"> 
    <cache usage="read-write"/> 
    <id name="Id" column="Id" type="int"> 
     <generator class="native" /> 
    </id> 
    <property name="Title" not-null="true" /> 
    <property name="Notes" /> 
    </class> 
</hibernate-mapping> 

Book模型

namespace Books.Models 
{ 
    public class Book 
    { 
     public virtual int Id { get; set; } 
     public virtual string Title { get; set; } 
     public virtual string Author { get; set; } 
     public virtual IList<Chapter> Chapters { get; set; } 
    } 
} 

章模型

namespace Books.Models 
{ 
    public class Chapter 
    { 
     public virtual int Id { get; set; } 
     public virtual string Title { get; set; } 
     public virtual string Notes { get; set; } 
    } 
} 

會話模型

namespace Books.Models 
{ 
    public class NHibernateSession 
    { 

     public static ISession OpenSessionBooks() 
     { 
      var configuration = new Configuration(); 
      var configurationPath = HttpContext.Current.Server.MapPath(@"~\Models\NHibernate\hibernate.cfg.xml"); 
      configuration.Configure(configurationPath); 
      var booksConfigurationFile = HttpContext.Current.Server.MapPath(@"~\Models\NHibernate\Book.hbm.xml"); 
      configuration.AddFile(booksConfigurationFile); 
      ISessionFactory sessionFactory = configuration.BuildSessionFactory(); 
      return sessionFactory.OpenSession(); 

     } 

    } 
} 

BookController的問題部分

public ActionResult Details(int id) 
     { 
      using (ISession session = NHibernateSession.OpenSessionBooks()) 
      { 
       var book = session.Get<Book>(id); 
       return View(book); 
      } 
     } 

在這裏,我期待着與加載圖書詳情所有章節列表的詳細信息。

的ActionResult詳細強類型的視圖:

@model Books.Models.Book 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Details</h2> 

<fieldset> 
    <legend>Book</legend> 

    <div class="display-label"> 
     @Html.DisplayNameFor(model => model.Title) 
    </div> 
    <div class="display-field"> 
     @Html.DisplayFor(model => model.Title) 
    </div> 

    <div class="display-label"> 
     @Html.DisplayNameFor(model => model.Author) 
    </div> 
    <div class="display-field"> 
     @Html.DisplayFor(model => model.Author) 
    </div> 

    <div class="display-label"> 
     @Html.DisplayNameFor(model => model.Chapters) 
    </div> 
    <div class="display-field"> 
     @Html.DisplayFor(model => model.Chapters) 
    </div> 

</fieldset> 
<p> 
    @Html.ActionLink("Edit", "Edit", new { id=Model.Id }) | 
    @Html.ActionLink("Back to List", "Index") 
</p> 

應用程序無法在視線

 @Html.DisplayNameFor(model => model.Chapters) 

隨着例外

Initializing[Books.Models.Book#7]-failed to lazily initialize a collection of role: Books.Models.Book.Chapters, no session or session was closed 

請幫我這個話題!解決方案.7z存檔可用here

回答

0

的原因是因爲提到Keith Nicholas是各分會延遲加載。有許多方法來解決這個問題,

1.使用DTO作爲模型

你可以使用一個BookDTO和因此,當視圖顯示了所有必要的模型ChapterDTO列表在地方數據

2.在該請求壽命

爲此,您需要爲每個在一個單一的會話綁定的請求,並在整個請求的生命時使用的請求方法使用會話的會話。請參閱this瞭解更多詳情。

2

您的章節被延遲加載。然而,當你的視圖被渲染時,它們只會被加載。屆時您的會議將關閉。

解決這個問題的兩種方法。

力當你有一個會話

負載離開會話打開網頁渲染也