2010-03-28 55 views
0

在我的追求進一步我的知識,我試圖讓NHibernate運行。NHibernate:沒有persister錯誤

我有以下結構,我的解決方案

  • 核心類庫項目
  • 基礎設施類庫項目
  • MVC應用程序項目
  • 測試項目

在我的核心項目我創建了以下實體:

using System; 

namespace Core.Domain.Model 
{ 
    public class Category 
    { 
     public virtual Guid Id { get; set; } 

     public virtual string Name { get; set; } 
    } 
} 

在我的基礎設施項目,我有以下映射:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
     namespace="Core.Domain.Model" 
     assembly="Core"> 

    <class name="Category" table="Categories" dynamic-update="true"> 
    <cache usage="read-write"/> 
    <id name="Id" column="Id" type="Guid"> 
     <generator class="guid"/> 
    </id> 
    <property name="Name" length="100"/> 
    </class> 
</hibernate-mapping> 

具有以下配置文件:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
    <property name="connection.connection_string">server=xxxx;database=xxxx;Integrated Security=true;</property> 
    <property name="show_sql">true</property> 
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> 
    <property name="cache.use_query_cache">false</property> 
    <property name="adonet.batch_size">100</property> 
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 
    <mapping assembly="Infrastructure" /> 
    </session-factory> 
</hibernate-configuration> 

在我的測試項目中,我有以下的測試

[TestMethod] 
     [DeploymentItem("hibernate.cfg.xml")] 
     public void CanCreateCategory() 
     { 
      IRepository<Category> repo = new CategoryRepository(); 
      Category category = new Category(); 
      category.Name = "ASP.NET"; 

      repo.Save(category); 

     } 

當我嘗試運行測試時出現以下錯誤:

測試方法Volunteer.Tests.CategoryTests.CanCreateCategory拋出異常:NHibernate.MappingException:無持久化:Core.Domain.Model.Category。

任何幫助將不勝感激。我確實將cfg構建操作設置爲嵌入式資源。

謝謝!

回答

0

XML映射文件的生成操作必須在Infrastructure組件設置爲Embedded Resource。在配置文件中使用以下指令時:<mapping assembly="Infrastructure" />它將在此程序集中查找作爲嵌入資源的映射。

+0

我做到了,並將相同的配置文件複製到我的測試程序集(它也設置爲嵌入式資源) – Mike 2010-03-28 12:48:48

+1

您不必嵌入配置文件,只需要映射。 – 2010-03-28 12:52:11

+0

就是這樣。不能相信我忽略了它。謝謝你的幫助! – Mike 2010-03-28 13:05:17

0

我猜問題是,你的配置在您的測試組件SessionFactory的沒有告訴它,你的映射是在你的核心組件,像

ISessionFactory factory = new Configuration().Configure() 
    .AddAssembly(typeof(Category).Assembly) <========*** 
    .BuildSessionFactory(); 

如果沒有幫助,發表您的配置碼。

乾杯,
Berryl

+0

我不知道該在哪裏添加該代碼完全在我的測試大會。我正在遵循http://forerunnerg34.wordpress.com/2009/11/03/using-asp-net-mvc-and-nhibernate-part-1/中找到的教程,並查看網站上下載的源文件,我沒有看到你發佈的任何內容。 謝謝 – Mike 2010-03-28 12:47:39

相關問題