gpt4 book ai didi

nhibernate - 当我删除父引用时,如何让 NHibernate 删除子引用?

转载 作者:行者123 更新时间:2023-12-03 12:08:17 25 4
gpt4 key购买 nike

我有一个 NewsFeed 对象映射如下:

<class name="NewsFeed">
<id name="NewsFeedId">
<generator class="guid"/>
</id>

<property name="FeedName" not-null="true" />
<property name="FeedURL" not-null="true" />
<property name="FeedIsPublished" not-null="true" />
</class>

可以拥有一组他们可能感兴趣的选定提要的用户,映射如下:
<class name="SystemUser">
<id name="SystemUserId">
<generator class="guid"/>
</id>


<set name="SelectedNewsFeeds" table="SystemUserSelectedNewsFeeds" cascade="all">
<key column="SystemUserId" />
<many-to-many column="NewsFeedId" class="NewsFeeds.NewsFeed, Domain"/>
</set>

</class>

我想要发生的是当我删除父 NewsFeed 时,所有 SelectedNewsFeed 引用也被删除,而不必加载每个 SystemUser 并手动删除 NewsFeed。

实现这一目标的最佳方法是什么?

更新 :在删除 NewsFeed 时,使用cascade="all-delete-orphan"而不是"all"仍然会导致异常:

The DELETE statement conflicted with the REFERENCE constraint "FKC8B9DF81601F04F4". The conflict occurred in database "System", table "dbo.SystemUserSelectedNewsFeeds", column 'NewsFeedId'.

最佳答案

JMCD

你的第二种方法:

Another alternative is to break the many-to-many relationship with a join class in the middle which nHiberate would be able to determine parent-child relationships and the cascade should work.



实际上是 nHibernate 人员在他们的文档中推荐的。

Don't use exotic association mappings.

Good usecases for a real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, we think that most associations are one-to-many and many-to-one, you should be careful when using any other association style and ask yourself if it is really neccessary.



使用两个一对多关联增加了将其他属性轻松添加到“订阅”的灵活性,例如该特定订阅的通知首选项。

关于nhibernate - 当我删除父引用时,如何让 NHibernate 删除子引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/444753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com