2009-05-28 55 views
2

這一個讓我撓我的頭,所以我希望第二雙眼睛可以幫助我在這裏。流利的NHibernate的自動映射錯誤

設置:

我有叫DomainEntity我所有的數據傳輸對象使用一個基類。它基本上只定義了一個名爲Id的屬性(它是一個整數)。

我已經得到了數據傳輸對象:博客,帖子,用戶 DomainEntity在命名空間Core.Domain,數據傳輸對象Core.Domain.Model

下,我有以下會議建設者代碼:

return Fluently.Configure() 
    .Database(SQLiteConfiguration.Standard.UsingFile("c:\blog.db")) 
    .Mappings(x => x.AutoMappings.Add(
     AutoPersistenceModel.MapEntitiesFromAssemblyOf<Blog>() 
      .Where(type => 
       type.Namespace.EndsWith("Domain.Model") && 
       !type.IsAbstract && 
       type.IsClass && 
       type.GetProperty("Id") != null)  
    )).BuildSessionFactory(); 

當我嘗試測試一個簡單的查詢,我得到上面的代碼(的地方)和錯誤消息的應用的例外是:

System.Applicat ionException:錯誤 而試圖建立映射 文獻爲 'Core.Domain.DomainEntity' ---> NHibernate.MappingException:無法 編譯映射文檔: (XmlDocument的)---> System.IndexOutOfRangeException :索引 超出了數組的範圍。

看來我的代碼/ NHibernate試圖映射DomainEntity,但失敗了。我以爲我的上面的代碼明確指出而不是通過使用type.Namespace.EndsWith(「Domain.Model」)來映射該對象。那是對的嗎?我在哪裏誤入歧途?

感謝您的幫助。

回答

3

貌似我忘了下面一行:

.WithSetup(a => a.IsBaseType = type => type == typeof(DomainEntity)) 

所以,在其全部,我的新的自動映射代碼如下所示:

return Fluently.Configure() 
       .Database(SQLiteConfiguration.Standard.UsingFile("c:\\blog.db")) 
       .Mappings(x => x.AutoMappings.Add(
        AutoPersistenceModel.MapEntitiesFromAssemblyOf<Blog>() 
         .WithSetup(a => a.IsBaseType = type => type == typeof(DomainEntity)) 
         .Where(type => 
          type.Namespace.EndsWith("Domain.Model") && 
          !type.IsAbstract && 
          type.IsClass && 
          type.GetProperty("Id") != null) 
        )).BuildSessionFactory(); 

這似乎已清除了錯誤權利了。