如何使用Nhibernate删除多个数据库实体?
这个问题的最佳做法是什么? 内置任何批处理function吗?
示例代码:
using (ITransaction transaction = _session.BeginTransaction()) { _session.Delete("FROM myObject o WHERE o.Id = IN(1,2,...99999)"); transaction.Commit(); }
提前致谢。
HQL支持IN子句,如果使用setParameterList,您甚至可以传入集合。
var idList = new List() { 5,3,6,7 }; _session.CreateQuery("DELETE myObject o WHERE o.Id = IN (:idList)") .SetParameterList("idList", idList) .ExecuteUpdate();
我有问题得到工作的答案,我发现以下查询100%工作
Session.CreateQuery("delete Customer c where c.id in (:deleteIds)") .SetParameterList("deleteIds", deleteIds) .ExecuteUpdate();
Customer是类名而不是表名。 id是小写的,在HQL中,它是主键而不是类中的属性名称(支持属性名称)
您可以使用HQL删除多个对象
在这里寻找删除 – 对于session.delete示例
HQL DELETE示例(您可以使用IN与HQL):
上述就是C#学习教程:如何使用Nhibernate删除多个数据库实体?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
ISession session = sessionFactory.OpenSession(); ITransaction tx = session.BeginTransaction(); String hqlDelete = "delete Customer c where c.name = :oldName"; // or String hqlDelete = "delete Customer where name = :oldName"; int deletedEntities = session.CreateQuery( hqlDelete ) .SetString( "oldName", oldName ) .ExecuteUpdate(); tx.Commit(); session.Close();
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/942722.html