Fluent NHibernate 一对多 Cascade.SaveUpdate() 阻止对实体的更新

发布于 2024-09-09 18:54:08 字数 1965 浏览 1 评论 0原文

我们有一个 Enrollment 对象,它有一个 Student 对象,而 Student 对象有许多 Enrollment 对象。如果我从 Enrollment 的 Student 引用中省略 Cascade.SaveUpdate(),则不会执行对 Student 表的更新,但对 Enrollment 对象的更新会成功。但是,如果我在 Enrollment 的 Student 引用上添加 Cascade.SaveUpdate(),则对 Student 表的更新工作正常,但对 Enrollment 表的更新失败。没有抛出异常,更新只是不成功。

一定有某种方法能够保存关系双方的对象,但我错过了什么?

这是代码片段,如果您需要更多,请告诉我:
EnrollmentMap:

    References(x => x.Student)
                .Column("student_id");// without the cascade on the next line, this fails to update changes to Student
                //.Cascade.SaveUpdate();// when uncommented this updates changes to Student but blocks updates to Enrollment

StudentMap:

    HasMany(x => x.Enrollments)
                .KeyColumn("student_id")
                .Inverse()
                .Cascade.SaveUpdate();

数据库调用:

public Application GetApplication(long applicationId)
        {
            using (var session = sessionFactory.OpenSession())
            {
                var query = session.Linq();
                query.Expand(x => x.Enrollment);
                query.Expand(x => x.Enrollment.Student);
                var result = from entity in query
                             where entity.ApplicationId == applicationId
                             select entity;
                return result.Count() > 0 ? result.First() : null;
            }
        }

数据库保存:

using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.SaveOrUpdate(entity);
                        transaction.Commit();
                    }
                    catch(Exception ex)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }

We have an Enrollment object that has a Student object and the Student object has many Enrollment objects. If I leave off the Cascade.SaveUpdate() from the Enrollment's Student reference, updates to the Student table do not execute, but updates to the Enrollment object succeed. But if I add the Cascade.SaveUpdate() on the Enrollment's Student reference, the updates to the Student table work fine, but updates to the Enrollment table fail. No exceptions are thrown, the updates just don't succeed.

There must be some way to be able to save objects on both sides of the relationship, but what am I missing?

Here's the code snips, let me know if you need more:
EnrollmentMap:

    References(x => x.Student)
                .Column("student_id");// without the cascade on the next line, this fails to update changes to Student
                //.Cascade.SaveUpdate();// when uncommented this updates changes to Student but blocks updates to Enrollment

StudentMap:

    HasMany(x => x.Enrollments)
                .KeyColumn("student_id")
                .Inverse()
                .Cascade.SaveUpdate();

Database call:

public Application GetApplication(long applicationId)
        {
            using (var session = sessionFactory.OpenSession())
            {
                var query = session.Linq();
                query.Expand(x => x.Enrollment);
                query.Expand(x => x.Enrollment.Student);
                var result = from entity in query
                             where entity.ApplicationId == applicationId
                             select entity;
                return result.Count() > 0 ? result.First() : null;
            }
        }

Database save:

using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.SaveOrUpdate(entity);
                        transaction.Commit();
                    }
                    catch(Exception ex)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

茶花眉 2024-09-16 18:54:08

您应该尝试在更新实体的同一会话中加载实体。我认为这是你的问题。

如果您确实无法做到这一点,那么可以将实体“合并”到您的会话中(谷歌“NHibernate merge”)。

You should try and load your entity in the same session as you update it. I think that is your problem.

If you really can't do this, then it is possible to 'merge' the entity in to your session (google 'NHibernate merge').

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