c# - Fluent NHibernate 自动映射错误

标签 c# nhibernate fluent-nhibernate automapping

这让我摸不着头脑,所以我希望第二双眼睛可以帮助我。

设置:

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

我有数据传输对象:博客、帖子、用户 DomainEntity位于命名空间Core.Domain中,数据传输对象位于Core.Domain.Model下

我有以下 session 生成器代码:

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: Error while trying to build the Mapping Document for 'Core.Domain.DomainEntity' ---> NHibernate.MappingException: Could not compile the mapping document: (XmlDocument) ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.

看来我的代码/NHibernate 正在尝试映射 DomainEntity,但失败了。我认为上面的代码明确指出使用 type.Namespace.EndsWith("Domain.Model") 来映射该对象。那是对的吗?我哪里会误入歧途?

感谢您的所有帮助。

最佳答案

看起来我忘记了以下行:

.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();

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

关于c# - Fluent NHibernate 自动映射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/922588/

相关文章:

c# - System.Security.Cryptography 中不存在 MD5 类

c# - TestScheduler 创建的 ColdObserver 在 OnCompleted 时不取消订阅?

c# - 在网络上获取图像的尺寸 - 避免内存占用?

nhibernate - 流利的Nhibernate-映射组件(值对象)的集合?

c# - 如何使用 fluent nhibernate 创建引用表

asp.net-mvc - 具有延迟加载问题的工作单元方法、nHibernate、ASP.NET MVC、CaSTLe Windsor

c# - 存储库中的异步 AsQueryable

nhibernate - 使用 NHibernate QueryOver,如何在日期之间添加类型安全限制

nhibernate - 将 IUserType 映射到流利的 NHibernate 中的组件属性

sql-server - Fluent Nhibernate 中的主键名称