Reputation: 57
I have the following mapping, but when i delete status entity the Nhibernate executes an update on child instead of delete.
public StatusMap()
{
Property(x => x.Date);
Property(x => x.Text, map => map.Type(NHibernateUtil.StringClob));
Property(x => x.Type);
ManyToOne(x => x.User, map => map.Column("UserId"));
Bag(x => x.Comments, map => { map.Key(km => km.Column("StatusId")); }, action => action.OneToMany());
Bag(x => x.Likes, map => { map.Key(km => km.Column("StatusId")); map.Cascade(Cascade.DeleteOrphans); }, action => action.OneToMany());
}
public StatusLikeMap()
{
ManyToOne(x => x.User, map => map.Column("UserId"));
ManyToOne(x => x.Status, map => { map.Column("StatusId"); });
Property(x => x.Date);
}
Delete(status) Results in this SQL:
UPDATE
CommentStatus
SET
StatusId = null
WHERE
StatusId = @p0;
@p0 = ce350d55-e476-4874-9e95-341cad9342e2 [Type: Guid (0)]
UPDATE
StatusLike
SET
StatusId = null
WHERE
StatusId = @p0;
@p0 = ce350d55-e476-4874-9e95-341cad9342e2 [Type: Guid (0)]
DELETE
FROM
Status
WHERE
Id = @p0;
@p0 = ce350d55-e476-4874-9e95-341cad9342e2 [Type: Guid (0)]
Any Idea ?? Help me please !
I made the change below and exclusion works, but this way the is database who fires the delete command.
How can I make nhibernate execute the delete instead of database ?
Bag(p => p.Likes, map =>
{
map.Key(k =>
{
k.Column("StatusId");
k.OnDelete(OnDeleteAction.Cascade);
});
map.Cascade(Cascade.DeleteOrphans);
map.Inverse(true);
}, ce => ce.OneToMany());
Upvotes: 1
Views: 256
Reputation: 64628
Cascade.DeleteOrphans only says that orphans get deleted. It doesn't mean that when the parent is deleted, the child is deleted as well.
You need to cascade "Remove" or "All" as well:
map.Cascade(Cascade.All | Cascade.DeleteOrphans)
Upvotes: 1