Nhibernate 3.x级联 - 做更新

时间:2012-05-22 03:55:01

标签: nhibernate nhibernate-mapping fluent-nhibernate-mapping

我有以下映射,但是当我删除状态实体时,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);
}

删除(状态)此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执行delete而不是数据库?

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

1 个答案:

答案 0 :(得分:1)

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

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

map.Cascade(Cascade.All | Cascade.DeleteOrphans)