流畅的NHibernate Automapping错误

时间:2009-05-28 18:56:28

标签: c# nhibernate fluent-nhibernate automapping

这个让我挠头,所以我希望第二双眼睛可以帮助我。

设定:

我有一个名为DomainEntity的基类,我的所有数据传输对象都使用它。它基本上只定义了一个名为Id的属性(它是一个整数)。

我有数据传输对象:Blog,Post,User 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.ApplicationException:错误   在尝试构建Mapping时   文件   'Core.Domain.DomainEntity'---&gt;   NHibernate.MappingException:不能   编译映射文档:   (XmlDocument)---&gt;   System.IndexOutOfRangeException:索引   超出了数组的范围。

似乎我的代码/ NHibernate试图映射DomainEntity,但是失败了。我认为上面的代码明确声明 not 使用type.Namespace.EndsWith(“Domain.Model”)来映射该对象。那是对的吗?我在哪里误入歧途?

感谢您的帮助。

1 个答案:

答案 0 :(得分: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();

这似乎已经清除了我的错误。