流畅的映射:使用连接表和级联

发布于 2024-10-02 12:22:51 字数 2050 浏览 2 评论 0原文

目前下表设置的映射遇到了一些问题:

               Shop
             [1]  [1]                 
            /       \
          [n]       [n]
   Category-[m]---[n]-Article

行为应该如下:
1 - 删除商店时,所有文章和类别都应删除
2 - 删除类别时,相关文章应取消分配但不删除
3 - 删除文章时,相关类别应取消分配但不删除

以下是当前映射:

public class ShopMap: ClassMap<Shop>
{
    public ShopMap()
    {
        this.Table("shop");
        Id(x => x.Id).Column("id").GeneratedBy.Native();
        Map(x => x.Name).Column("name");

        HasMany(x => x.Categories).Cascade.AllDeleteOrphan;
        HasMany(x => x.Articles).Cascade.AllDeleteOrphan;
    }
}

public class CategoryMap: ClassMap<Category>
{
    public CategoryMap()
    {
        this.Table("category");
        Id(x => x.Id).Column("id").GeneratedBy.Native();
        Map(x => x.Name).Column("name");

        References(x => x.Shop);

        HasManyToMany(x => x.Articles).Cascade.AllDeleteOrphan()
                                    .Table("article_category")
                                    .ChildKeyColumn("article_id")
                                    .ParentKeyColumn("category_id")
                                        .Inverse();
    }
}

public class ArticleMap: ClassMap<Article>
{
    public ArticleMap()
    {
        this.Table("article");
        Id(x => x.Id).Column("id").GeneratedBy.Native();
        Map(x => x.Name).Column("name");

        References(x => x.Shop);

        HasManyToMany(x => x.Categories).Cascade.All()
                                    .Table("article_category")
                                    .ParentKeyColumn("article_id")
                                    .ChildKeyColumn("category_id");
    }
}

删除类别 (Session.Delete()) 时,NH 也会尝试删除相关文章。将 Cascade-Mode 更改为 SaveUpdate 将解决此问题,但会将条目保留在链接表 *article_category* 中。总结:Cascade.SaveUpdate 太懒了,Cascade.All 太急切了。

我尝试了映射中想到的所有内容,但找不到正确的方法来映射它(相当简单的模式)。

任何有关如何(流畅地)绘制此图的想法都将不胜感激!

预先感谢

塞比

having a little trouble with a mapping for the following table setup currently:

               Shop
             [1]  [1]                 
            /       \
          [n]       [n]
   Category-[m]---[n]-Article

The behaviour should be the following :
1 - when deleting a shop, all Articles and Categories Should be deleted
2 - when deleting a Category, related Articles should be unassigned but not deleted
3 - when deleting an Article, related Categories should be unassigned but not deleted

Here's the current mapping:

public class ShopMap: ClassMap<Shop>
{
    public ShopMap()
    {
        this.Table("shop");
        Id(x => x.Id).Column("id").GeneratedBy.Native();
        Map(x => x.Name).Column("name");

        HasMany(x => x.Categories).Cascade.AllDeleteOrphan;
        HasMany(x => x.Articles).Cascade.AllDeleteOrphan;
    }
}

public class CategoryMap: ClassMap<Category>
{
    public CategoryMap()
    {
        this.Table("category");
        Id(x => x.Id).Column("id").GeneratedBy.Native();
        Map(x => x.Name).Column("name");

        References(x => x.Shop);

        HasManyToMany(x => x.Articles).Cascade.AllDeleteOrphan()
                                    .Table("article_category")
                                    .ChildKeyColumn("article_id")
                                    .ParentKeyColumn("category_id")
                                        .Inverse();
    }
}

public class ArticleMap: ClassMap<Article>
{
    public ArticleMap()
    {
        this.Table("article");
        Id(x => x.Id).Column("id").GeneratedBy.Native();
        Map(x => x.Name).Column("name");

        References(x => x.Shop);

        HasManyToMany(x => x.Categories).Cascade.All()
                                    .Table("article_category")
                                    .ParentKeyColumn("article_id")
                                    .ChildKeyColumn("category_id");
    }
}

When deleting a Category (Session.Delete()), NH tries to delete the related Articles as well. Changing the Cascade-Mode to SaveUpdate will fix this, but will leave the entries in the link table *article_category*. Summing up : Cascade.SaveUpdate is too lazy, Cascade.All is too eager.

I tried everything that came to my mind in the mappings, but couldn't find a correct way to map this (rather simple schema).

Any ideas on how to (fluently) map this are greatly appreciated!

Thanks in advance

Sebi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海里的那抹蓝 2024-10-09 12:22:51

这些条目保留在链接表中,因为 Category.Articles 被定义为关系的反面。您需要先从 Article.Categories 中删除该类别,然后再删除它,以便删除链接记录。

The entries are left in the link table because Category.Articles is defined as the inverse side of the relationship. You need to remove the Category from Article.Categories before deleting it in order for the link record to be removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文