这让我摸不着头脑,所以我希望第二双眼睛可以帮助我。
设置:
我有一个名为 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/