How can one delete NHibernate objects using a criteria?

前端 未结 4 1359
旧巷少年郎
旧巷少年郎 2020-12-15 23:28

This must be a simple question. Given a criteria, how one deletes the entities satisfying the criteria?

The rationale:

HQL and NH criteria

4条回答
  •  隐瞒了意图╮
    2020-12-16 00:15

    I know this is an old question but for argument sake; if one uses repository pattern you can declare a delete method which does the following:

    public void Delete(System.Linq.Expressions.Expression> predicate)
    {
        var entities = _session.Query().Where(predicate);
        foreach (var entity in entities)
            _session.Delete(entity);
    }
    

    Note the code is using expressions in order for repository interface to be generic enough so you can also implement a for example Entity Framework repository.

提交回复
热议问题