遗留映射中的 NHibernate 映射错误

发布于 2024-11-18 19:55:35 字数 1140 浏览 1 评论 0原文

我继承了现有功能应用程序中的大量 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-11-25 19:55:35

我已经解决了这个问题 - 由于某种原因,NHibernate 不喜欢在项目映射中内联指定组件映射,但如果我将映射移动到单独的类 ComponentMap 它工作了。因此,我将有问题的行更改为

Component(p => p.ProjectModules);

并将以下类添加到我的映射程序集中:

public class ProjectModulesMap : ComponentMap<ProjectModules>
{
    LazyLoad.Always();
    Map(pm => pm.ModuleOne);
    Map(pm => pm.ModuleTwo);
}

然后一切都按照我从一开始就预期的方式工作。

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 to

Component(p => p.ProjectModules);

and added the following class to my mappings assembly:

public class ProjectModulesMap : ComponentMap<ProjectModules>
{
    LazyLoad.Always();
    Map(pm => pm.ModuleOne);
    Map(pm => pm.ModuleTwo);
}

Then everything worked as I would have expected it to from the start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文