与 Castle ActiveRecord HasAndBelongsToMany 进行级联

发布于 2024-07-18 23:20:02 字数 1467 浏览 7 评论 0原文

我在使用 Castle ActiveRecord/NHibernate 通过 HasAndBelongsToMany 关系进行级联删除时遇到很多麻烦。

我的照片具有并属于许多标签。 它们由一个名为 PhotoHasTag 的表连接起来,该表只有 photoId 和 tagId。 当我删除照片时,我希望删除所有关联的 PhotoHasTag 条目,并删除所有孤立的标签。

现在我的照片类设置如下:

    [ActiveRecord(Table="Photo")]
    public class Photo
    {
        [PrimaryKey(Column = "photoId", Generator = Castle.ActiveRecord.PrimaryKeyType.Identity)]
        public virtual int Id { get; set; }    

        [HasAndBelongsToMany(Table="PhotoHasTag",ColumnKey="photoId",ColumnRef="tagId",Lazy=true,Cascade=ManyRelationCascadeEnum.AllDeleteOrphan)]
        public virtual IList<Tag> Tags { get; set; }
    }

我的标签类的设置方式几乎相同:

    [ActiveRecord(Table="Tag")]
    public class Photo
    {
        [PrimaryKey(Column = "tagId", Generator = Castle.ActiveRecord.PrimaryKeyType.Identity)]
        public virtual int Id { get; set; }  

        [HasAndBelongsToMany(Table = "PhotoHasTag", ColumnKey = "tagId", ColumnRef = "photoId", Lazy = true)]
        public IList<Photo> Photos { get; set; }
    }

当我尝试删除照片时,我最终从 SQL Server 收到错误:

The DELETE statement conflicted with the REFERENCE constraint "PhotoHasTag_FK1". 

我可以通过设置在 SQL Server 中解决此问题Key 上的删除规则为 Cascade,但随后仅删除 PhotoHasTag。 如果存在任何孤立标签,它们仍将在数据库中徘徊。

我正在考虑编写一个触发器来处理剩余的标签,但如果有一种方法可以设置我的 ActiveRecord 映射以便删除级联下来,我会更高兴。

预先感谢,我一直坚持这个问题,所以我感谢任何和所有的帮助!

I'm having a lot of trouble cascading deletes through a HasAndBelongsToMany relationship using Castle ActiveRecord/NHibernate.

I have Photos which have and belong to many Tags. They are joined by a table called PhotoHasTag which just has a photoId and tagId. When I delete a Photo, I'd like all associated PhotoHasTag entries to be deleted, and any then orphaned Tags to be deleted as well.

Right now I have my Photo class setup something like this:

    [ActiveRecord(Table="Photo")]
    public class Photo
    {
        [PrimaryKey(Column = "photoId", Generator = Castle.ActiveRecord.PrimaryKeyType.Identity)]
        public virtual int Id { get; set; }    

        [HasAndBelongsToMany(Table="PhotoHasTag",ColumnKey="photoId",ColumnRef="tagId",Lazy=true,Cascade=ManyRelationCascadeEnum.AllDeleteOrphan)]
        public virtual IList<Tag> Tags { get; set; }
    }

And my Tag class is setup pretty much the same way:

    [ActiveRecord(Table="Tag")]
    public class Photo
    {
        [PrimaryKey(Column = "tagId", Generator = Castle.ActiveRecord.PrimaryKeyType.Identity)]
        public virtual int Id { get; set; }  

        [HasAndBelongsToMany(Table = "PhotoHasTag", ColumnKey = "tagId", ColumnRef = "photoId", Lazy = true)]
        public IList<Photo> Photos { get; set; }
    }

When I try to remove photo's I end up getting an error from SQL Server:

The DELETE statement conflicted with the REFERENCE constraint "PhotoHasTag_FK1". 

I can step around this in SQL Server by setting the Delete Rule on the Key to Cascade, but then only the PhotoHasTag is deleted. If there are any orphaned Tags they will still be lingering in the database.

I am considering writing a Trigger to take care of the left over Tags, but I would be much happier if there is a way to setup my ActiveRecord mappings so that the delete cascades down.

Thanks in advance, I've been stuck on this forever so I appreciate any and all help!

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

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

发布评论

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

评论(1

迷离° 2024-07-25 23:20:02

您可以在属性中指定级联行为,如下所示:

[HasAndBelongsToMany(Table = "PhotoHasTag", Cascade=ManyRelationCascadeEnum.AllDeleteOrphan, ColumnKey = "tagId", ColumnRef = "photoId", Lazy = true)]
public IList<Photo> Photos { get; set; }

API 文档可能更有帮助。

You can specify cascade behaviour in the attributes like so:

[HasAndBelongsToMany(Table = "PhotoHasTag", Cascade=ManyRelationCascadeEnum.AllDeleteOrphan, ColumnKey = "tagId", ColumnRef = "photoId", Lazy = true)]
public IList<Photo> Photos { get; set; }

The API documentation is probably more helpful.

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