如何在nhibernate中使用PK删除对象? [英] How to delete an object by using PK in nhibernate?

查看:61
本文介绍了如何在nhibernate中使用PK删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除一个对象而不先从数据库中获取它?

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

在另一个ORM中,我可以这样做:

In another ORM, I can do this:

session.Delete<User>(1); // 1 = PK

推荐答案

将以下类添加到您的项目中:

Add the following class to your project:

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

您现在可以使用session.Delete<User>(1).

这篇关于如何在nhibernate中使用PK删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆