与 Castle ActiveRecord HasAndBelongsToMany 进行级联
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在属性中指定级联行为,如下所示:
API 文档可能更有帮助。
You can specify cascade behaviour in the attributes like so:
The API documentation is probably more helpful.