HasManyToMany功能NHibernate映射删除错误 [英] HasManyToMany Fluent NHibernate Mapping Delete Error

查看:261
本文介绍了HasManyToMany功能NHibernate映射删除错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在一个多对多的映射某个实体我所说的任务。一个任务可以有很多孩子和很多家长。有一个在它们之间只是有两个FK列ParentTaskId和ChildTaskId连接表。这里是我想出迄今

I have been working on a many to many mapping on an entity i have called Task. A task can have many children and many parents. There is a join table in between which just has the two FK columns "ParentTaskId" and "ChildTaskId". Here is what I have come up with so far.

        // Many-to-Many Parents
        HasManyToMany<Task>(x => x.Parents)
            .Table("TaskDependency")
            .ParentKeyColumn("ParentTaskId")
            .ChildKeyColumn("ChildTaskId")
            .Inverse()
            .Not.LazyLoad()
            .Cascade.SaveUpdate();

        // Many-to-Many Children
        HasManyToMany<Task>(x => x.Children)
            .Table("TaskDependency")
            .ParentKeyColumn("ChildTaskId")
            .ChildKeyColumn("ParentTaskId")
            .Not.LazyLoad()
            .Cascade.SaveUpdate();

这是一些示例代码,以帮助我说明我的问题。下面的代码工作:

And here is some example code to help me illustrate my question. The code below works:

        _taskRepository.Save(_taskVendor);
        _taskRepository.Save(_taskClientVendor);
        _taskRepository.Save(_taskClient);

        _taskVendor.Children.Add(_taskClientVendor);
        _taskClientVendor.Children.Add(_taskClient);
        _taskRepository.Save(_taskVendor);
        _taskRepository.Save(_taskClientVendor);

        _taskRepository.Delete(_taskVendor);
        _taskRepository.Delete(_taskClientVendor);
        _taskRepository.Delete(_taskClient);



但是,如果我改变delete语句的顺序是:

But if I change the order of the delete statements to be:

        _taskRepository.Delete(_taskVendor);
        _taskRepository.Delete(_taskClient);
        _taskRepository.Delete(_taskClientVendor);



我得到一个FK约束违反了我的多对多的连接表。我想这与我有逆建立在我的映射方式做。它的影响在其中查询执行,以避免这个确切FK约束问题的顺序。有什么办法来映射,这样我可以在任何一方,子女或父母删除实体,而不必此异常?我想在我的映射两侧逆但只是导致我的多对多的关系没有得到保存。 > _<

I get a fk constraint violation on my many to many join table. I think this has to do with the way I have inverse set up on my mapping. It effects the order in which queries are executed to avoid this exact fk constraint issue. Is there any way to map this so that I could delete entities on either side, child or parent, without having this exception? I tried inverse on both sides of my mapping but that just resulted in my many to many relationship not getting saved. >_<

任何帮助将非常感激。

推荐答案

您必须手动删除它们。

void Delete(Task task)
{
    foreach (var parent in task.Parents)
    {
        parent.Childs.Remove(task);
    }
    session.Delete(task);
}

这篇关于HasManyToMany功能NHibernate映射删除错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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