NHibernate 简单删除让我发疯
我正在尝试删除一个实体(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
Inverse()
添加到 HasMany 映射中。Try adding
Inverse()
to the HasMany mapping.