I have a problem that is driving me crazy. I’ve been searching around and found some possible solutions, but none with any result. I’ll try to explain as good as I can.
I have two models, PostModel (parent) and CommentModel (child). For better understanding, my mapping looks like this:
Mapping for Post:
public class PostMap : ClassMap<PostModel>
{
public PostMap()
{
// Table
Table("Post");
// Relations
HasMany<CommentModel>(o => o.Comments).KeyColumn("PostId").Inverse().Cascade.AllDeleteOrphan();
}
}
Mapping for comment:
public class CommentMap : ClassMap<CommentModel>
{
public CommentMap()
{
// Table
Table("Comment");
// References
References<PostModel>(o => o.Post).Column("PostId");
}
}
Now, what I’m trying to do is to delete a comment entity from post like this:
public void DeleteComment(CommentModel commentToDelete)
{
// Delete from list
commentToDelete.Post.Comments.Remove(commentToDelete);
// Update parent
Update(commentToDelete.Post);
}
The result? Nothing is happening. Nothing is deleted from the list, no exceptions or what so ever. Does anyone have a solution for this, or any thoughts? I really appreciate all help I can get.
TIA!
If you set Inverse() you will have to update your comment
I suggest this approach:
If you always remove your associations in both objects, you should not have problems.
You can also remove the Inverse