Fluent NHibernate:ManyToMany 自引用映射

2024-04-10

我需要帮助为这种情况创建正确的流畅 nh 映射:

类别可以是一个或多个类别的子类别。因此,导致该实体:

public class Category : Entity, IAggregateRoot
{
    [EntitySignature]
    public virtual string Name { get; set; }
    public virtual IList<Category> Parents { get; set; }
    public virtual IList<Category> Children { get; set; }
    public virtual IList<ProductCategory> Products { get; set; }

    public Category()
    {
        Parents = new List<Category>();
        Children = new List<Category>();
        Products = new List<ProductCategory>();

    }

    public virtual void AddCategoryAsParent(Category parent)
    {
        if (parent != this && !parent.Parents.Contains(this) && !Parents.Contains(parent))
        {
            Parents.Add(parent);
            parent.AddCategoryAsChild(this);
        }
    }

    public virtual void RemoveCategoryAsParent(Category parent)
    {
        if (Parents.Contains(parent))
        {
            Parents.Remove(parent);
            parent.RemoveCategoryAsChild(this);
        }
    }

    public virtual void AddCategoryAsChild(Category child)
    {
        if(child != this && !child.Children.Contains(this) && !Children.Contains(child))
        {
            Children.Add(child);
            child.AddCategoryAsParent(this);
        }
    }

    public virtual void RemoveCategoryAsChild(Category child)
    {
        if(Children.Contains(child))
        {
            Children.Remove(child);
            child.RemoveCategoryAsParent(this);
        }
    }
}

我最初的映射是这样的:

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Id(p => p.Id).GeneratedBy.Identity();

        HasManyToMany(x => x.Parents)
            .Table("CategoryParents")
            .ParentKeyColumn("CategoryId")
            .ChildKeyColumn("ParentCategoryId")
            .Cascade.SaveUpdate()
            .LazyLoad()
            .AsBag();

        HasManyToMany(x => x.Children)
            .Table("CategoryParents")
            .ParentKeyColumn("ParentCategoryId")
            .ChildKeyColumn("CategoryId")
            .Cascade.SaveUpdate()
            .Inverse()
            .LazyLoad()
            .AsBag();
    }
}

此映射的问题是,每当我删除一个类别作为另一个类别的父类别或子类别时,生成的 SQL 语句如下:

NHibernate: DELETE FROM CategoryParents WHERE CategoryId = @p0;@p0 = 2
NHibernate: INSERT INTO CategoryParents (CategoryId, ParentCategoryId) VALUES (@p0, @p1);@p0 = 2, @p1 = 3

它首先删除所有映射,然后插入剩余的映射。正确的方法是删除此类语句的类别父映射:

DELETE FROM CategoryParents WHERE CategoryId = @p0 AND ParentCategoryId = @p1;@p0 = 2, @p1=1

有任何想法吗?


查看您的映射后,我认为您想要更改级联选项。这是一篇概述实体之间的父子关系的文章。尽管它是从拥有孤立实体的角度出发的,但我认为您会发现该博客很有帮助。祝你好运...

http://ayende.com/Blog/archive/2006/12/02/nhibernatecascadesthe Different Betweenallalldeleteorphansandsaveupdate.aspx http://ayende.com/Blog/archive/2006/12/02/nhibernatecascadesthedifferentbetweenallalldeleteorphansandsaveupdate.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Fluent NHibernate:ManyToMany 自引用映射 的相关文章

随机推荐