1

我有以下映射,但是当我删除状态实体时,Nhibernate 会对子项执行更新而不是删除。

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) 在此 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)]

任何想法 ??请帮帮我 !

编辑:

我在下面进行了更改并且排除工作,但是这样是触发删除命令的数据库。

如何让 nhibernate 执行删除而不是数据库?

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());
4

1 回答 1

1

Cascade.DeleteOrphans 只说孤儿被删除。这并不意味着当父级被删除时,子级也被删除。

您还需要级联“删除”或“全部”:

map.Cascade(Cascade.All | Cascade.DeleteOrphans)
于 2012-05-22T05:52:12.347 回答