遗留映射中的 NHibernate 映射错误
我继承了现有功能应用程序中的大量 NHibernate 映射。我已经对该应用程序进行了分支来开发一些新功能,同时我还扩展了测试基础架构以允许采用更像 TDD 的方法。但现在我在一个集成测试中遇到了障碍......
我有一个包含测试数据的类,我在集成测试之前插入了该数据。在插入这些的方法中,我得到以下异常:
NHibernate.PropertyAccessException:无效转换(检查映射是否属性类型不匹配); Domain.Entities.Project 的设置者 ---> System.InvalidCastException:无法将“System.Object”类型的对象转换为“Domain.Entities.ProjectModules”类型。
我不明白为什么。我有两个 Project
实例,我尝试在设置时将其保留在数据库中,它们的定义如下:
new Project("2023", "projeName", "projaddr")
{
PrincipalOwner = UserOne, // UserOne and Office are other properties
Office = Office,
// I've tried just not instantiating this too - gave the same exception
ProjectModules = new ProjectModules
{
HasModuleOne = false,
HasModuleTwo = false
});
});
Fluent NHibernate 映射(的相关部分)如下所示:
Component(m => m.ProjectModules, c =>
{
c.LazyLoad();
c.Map(x => x.HasModuleOne)
.Column("ModuleOne").Not.Nullable().Default("0");
c.Map(x => x.HasModuleTwo)
.Column("ModuleTwo").Not.Nullable().Default("0");
});
I've inherited a large set of NHibernate mappings that live in an existing, functional application. I've branched this application to develop some new features, and while I do so I'm also extending the testing infrastructure to allow for a more TDD-like approach. But now I've hit a wall in one of my integration tests...
I have a class with test data, which I insert prior to the integration test. In the method that inserts these, I get the following exception:
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Domain.Entities.Project ---> System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'Domain.Entities.ProjectModules'.
and I can't figure out why. I have two Project
instances that I try to persist in the database on setup, both defined like this:
new Project("2023", "projeName", "projaddr")
{
PrincipalOwner = UserOne, // UserOne and Office are other properties
Office = Office,
// I've tried just not instantiating this too - gave the same exception
ProjectModules = new ProjectModules
{
HasModuleOne = false,
HasModuleTwo = false
});
});
The (relevant part of the) Fluent NHibernate mapping looks like this:
Component(m => m.ProjectModules, c =>
{
c.LazyLoad();
c.Map(x => x.HasModuleOne)
.Column("ModuleOne").Not.Nullable().Default("0");
c.Map(x => x.HasModuleTwo)
.Column("ModuleTwo").Not.Nullable().Default("0");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题 - 由于某种原因,NHibernate 不喜欢在项目映射中内联指定组件映射,但如果我将映射移动到单独的类
ComponentMap
它工作了。因此,我将有问题的行更改为并将以下类添加到我的映射程序集中:
然后一切都按照我从一开始就预期的方式工作。
I've solved this - for some reason, NHibernate didn't like when the component mapping was specified inline in the mapping for Projects, but if I moved the mapping to a separate class
ComponentMap<T>
it worked. So I changed the problematic lines toand added the following class to my mappings assembly:
Then everything worked as I would have expected it to from the start.