NHibernate 简单删除让我发疯

发布于 2024-09-14 05:47:35 字数 1004 浏览 4 评论 0原文

我正在尝试删除一个实体(ForumTopic)并删除其中的帖子(ForumPost)。这是我的实体:

public class ForumTopic
{
    public virtual int TopicID { get; set; }
    public virtual IList<ForumPost> Posts { get; private set; }

    ...

    public ForumTopic()
    {
        Posts = new List<ForumPost>();
    }
}

public class ForumPost
{
    public virtual int PostID { get; set; }
    public virtual ForumTopic Topic { get; set; }

    ...
}

具有以下映射:

public ForumTopicMap()
{
    Table("ForumTopics");
    Id(x => x.TopicID);
    HasMany(x => x.Posts)
        .Cascade.All();

    ...
}

public ForumPostMap()
{
    Table("ForumPosts");
    Id(x => x.PostID);
    References(x => x.Topic, "TopicID");

    ...
}

但是,当我删除我的主题时,我收到以下错误:

[ForumTopic.Posts#14][SQL: UPDATE ForumPosts SET TopicID = null WHERE TopicID = @p0]

正如我所想,这似乎很奇怪通过在我的 HasMany 映射上说 Cascade.All() (我事件尝试了 Cascade.Delete()),它将删除该主题的所有帖子。如果有人能告诉我我做错了什么,我将不胜感激。谢谢

I'm trying to delete an entity (ForumTopic) and have that delete the Posts (ForumPost) within it. Here are my entities:

public class ForumTopic
{
    public virtual int TopicID { get; set; }
    public virtual IList<ForumPost> Posts { get; private set; }

    ...

    public ForumTopic()
    {
        Posts = new List<ForumPost>();
    }
}

public class ForumPost
{
    public virtual int PostID { get; set; }
    public virtual ForumTopic Topic { get; set; }

    ...
}

With the following mappings:

public ForumTopicMap()
{
    Table("ForumTopics");
    Id(x => x.TopicID);
    HasMany(x => x.Posts)
        .Cascade.All();

    ...
}

public ForumPostMap()
{
    Table("ForumPosts");
    Id(x => x.PostID);
    References(x => x.Topic, "TopicID");

    ...
}

However when i delete my topic i receive the following error:

[ForumTopic.Posts#14][SQL: UPDATE ForumPosts SET TopicID = null WHERE TopicID = @p0]

This seems strange as I thought by saying Cascade.All() (I event tried Cascade.Delete()) on my HasMany mapping it would delete all the posts for this topic. I'd appreciate it if someone could show me what i'm doing wrong. Thanks

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

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

发布评论

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

评论(1

梦途 2024-09-21 05:47:35

尝试将 Inverse() 添加到 HasMany 映射中。

Try adding Inverse() to the HasMany mapping.

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