How to delete an object by using PK in nhibernate?

后端 未结 5 1272

How do I delete an object without fetching it from the db first?

In another ORM, I can do this:

session.Delete(1); // 1 = PK
5条回答
  •  执笔经年
    2021-02-02 17:09

    Add the following class to your project:

    public static class SessionHelper
    {
        public static void Delete(this ISession session, object id)
        {
            var queryString = string.Format("delete {0} where id = :id",
                                            typeof(TEntity));
            session.CreateQuery(queryString)
                   .SetParameter("id", id)
                   .ExecuteUpdate();
        }
    }
    

    You can now use session.Delete(1).

提交回复
热议问题