删除级联不能使用NHibernate [英] Delete Cascade is not working with NHibernate
问题描述
在PersonCompany的映射中,我为此引用定义了级联删除:
pre $
this.HasMany(x = > x.Communications)
.AsSet()
.KeyColumn(PersonCompanyId)
.Fetch.Select()
.Inverse()
.Cascade。删除();
但是当我现在执行下面的HQL查询时:
$ b $(< pre> var sql =从PersonCompany中删除Person.Id in(:idList)或Company.Id in(:idList);
与
var query = NHibernateHelper.CurrentSession.CreateQuery(sql);
query.SetParameterList(idList,contactIdList);
query.SetTimeout(0);
query.ExecuteUpdate();
我总是得到这个SqlException:
DELETE语句与REFERENCE约束FK_PersonCompany_Communication冲突。冲突发生在数据库proconact,表dbo.Communication,列PersonCompanyId。
语句已被终止。
我认为NHibernate现在应该删除级联通信中的引用记录 - should'nt是吗?
我希望有人能帮助我,我在做什么错。
,这实际上是用来在数据库服务器上进行BULK操作的。它们使用HQL语法,但是不包括级联(因为它们不在内存中执行,只是在DB端)
这样,我们可以加载对象,被删除...并明确删除它们。这将触发级联:
// var sql =从PersonCompany中删除Person.Id在(:idList)或Company中。 Id(:idList);
var sql =从PersonCompany其中Person.Id在(:idList)或Company.Id在(:idList);
var query = NHibernateHelper.CurrentSession.CreateQuery(sql);
query.SetParameterList(idList,contactIdList);
query.SetTimeout(0);
//query.ExecuteUpdate();
var list = query.List< PersonCompany>();
foreach(var list in list)
{
session.Delete(item);
}
session.Flush();
发生了什么事情,即将每个项目都删除,并放置在的ISession
。在 Delete()
过程中,所有的级联都被正确执行了。
$ b
I have a table Communication which has a reference to PersonCompany. In the mapping for PersonCompany i have defined a Cascade-Delete for this reference:
this.HasMany(x => x.Communications)
.AsSet()
.KeyColumn("PersonCompanyId")
.Fetch.Select()
.Inverse()
.Cascade.Delete();
But when I now execute the fallowing HQL-Query:
var sql = "delete from PersonCompany where Person.Id in (:idList) or Company.Id in (:idList)";
with
var query = NHibernateHelper.CurrentSession.CreateQuery(sql);
query.SetParameterList("idList", contactIdList);
query.SetTimeout(0);
query.ExecuteUpdate();
I always get this SqlException:
The DELETE statement conflicted with the REFERENCE constraint "FK_PersonCompany_Communication". The conflict occurred in database "proconact", table "dbo.Communication", column 'PersonCompanyId'. The statement has been terminated.
I think, NHibernate should now delete cascade the referenced records in Communication - should'nt it?
I hope someone can help me, what I am doing wrong.
The syntax you've used is in fact part of the
which are in fact used to BULK operation on the DB server. They use the HQL syntax, but do not cover the cascade (because they are not executed in memory, just on the DB side)
This way, we can load the objects, to be deleted... and explicitly delete them. This will trigger cascades:
//var sql = "delete from PersonCompany where Person.Id in (:idList) or Company.Id in (:idList)";
var sql = "from PersonCompany where Person.Id in (:idList) or Company.Id in (:idList)";
var query = NHibernateHelper.CurrentSession.CreateQuery(sql);
query.SetParameterList("idList", contactIdList);
query.SetTimeout(0);
//query.ExecuteUpdate();
var list = query.List<PersonCompany >();
foreach (var item in list)
{
session.Delete(item);
}
session.Flush();
What happened is, that each and every item to be deleted, and placed in ISession
. During the Delete()
all the cascades were properly executed
这篇关于删除级联不能使用NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!