使用 Fluent NHibernate 的 AutoPersistenceModel 但在单个对象中预先加载
我正在使用 Fluent NHibernate 来自动映射我的实体。
这是我用于自动映射的代码:
new AutoPersistenceModel()
.AddEntityAssembly(Assembly.GetAssembly(typeof(Entity)))
.Where(type => type.Namespace.Contains("Domain") && type.BaseType != null && type.BaseType.Name.StartsWith("DomainEntity") && type.BaseType.IsGenericType == true)
.WithSetup(s => s.IsBaseType = (type => type.Name.StartsWith("DomainEntity") && type.IsGenericType == true))
.ConventionDiscovery.Add(
ConventionBuilder.Id.Always(x => x.GeneratedBy.Increment())
);
这工作得很好。 但现在我需要在我的域的一个对象中进行预加载。 找到此答案。 但是,当我将行 .ForTypesThatDeriveFrom
添加到代码中并运行它时,出现以下异常:
- 尝试执行时出错为 IEagerLoading 构建映射文档
请注意,我正在使用一个接口 (IEagerLoading
) 来标记我想要预先加载的对象。
谁能帮助如何做到这一点? 请记住,我想保留自动映射功能。
谢谢
I'm using Fluent NHibernate in order to auto map my entities.
This is the code I'm using for the auto mapping:
new AutoPersistenceModel()
.AddEntityAssembly(Assembly.GetAssembly(typeof(Entity)))
.Where(type => type.Namespace.Contains("Domain") && type.BaseType != null && type.BaseType.Name.StartsWith("DomainEntity") && type.BaseType.IsGenericType == true)
.WithSetup(s => s.IsBaseType = (type => type.Name.StartsWith("DomainEntity") && type.IsGenericType == true))
.ConventionDiscovery.Add(
ConventionBuilder.Id.Always(x => x.GeneratedBy.Increment())
);
This works just fine. But now I need to have Eager Loading in one single object of my domain. Found this answer. But when I add the line .ForTypesThatDeriveFrom<IEagerLoading>(map => map.Not.LazyLoad())
to the code and run it I get the following exception:
- Error while trying to build the Mapping Document for IEagerLoading
Notice that I'm using an interface (IEagerLoading
) to mark the objects that I want eager load.
Can anyone help how to do this? Remember that I want to keep the auto mapping functionality.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题是
ForTypesThatDeriveFrom
的命名有点误导性,它的真正含义是ForMappingsOf
,所以它试图找到一个>ClassMap
显然不存在。我相信您应该能够使用自定义的
IClassConvention
来处理这个问题。 这超出了我的想象,但应该有效:The problem you're hitting is that
ForTypesThatDeriveFrom<T>
is a bit misleadingly named, and that it really meansForMappingsOf<T>
, so it's trying to find aClassMap<IEagerLoading>
which obviously doesn't exist.I believe you should be able to handle this with a custom
IClassConvention
. This is off the top of my head, but should work: