Fluent-NHibernate:如何创建具有独特约束的多对多关系

发布于 2024-08-11 03:19:33 字数 330 浏览 2 评论 0原文

我想创建多对多关系,但我想在新表(MessageReceivers)中对两列(AdvanceMessageId,UserId)有一个唯一的约束:

mapping.HasManyToMany(x => x.Receivers)
       .WithParentKeyColumn("AdvanceMessageId")
       .WithChildKeyColumn("UserId")
       .Cascade.All()
       .LazyLoad()
       .WithTableName("MessageReceivers");

感谢帮助

I want to create a many to many relationship, but I want to have in the new table(MessageReceivers) a unique contraint on both columns (AdvanceMessageId,UserId):

mapping.HasManyToMany(x => x.Receivers)
       .WithParentKeyColumn("AdvanceMessageId")
       .WithChildKeyColumn("UserId")
       .Cascade.All()
       .LazyLoad()
       .WithTableName("MessageReceivers");

Thanks for help

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

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

发布评论

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

评论(2

总攻大人 2024-08-18 03:19:33

旧帖子...但万一其他人到达这里寻找答案:

您需要将 .AsSet() 添加到 HasManyToMany 映射定义中。


即,

mapping.HasManyToMany(x => x.Users)
       .WithTableName("MessageReceivers")
       .WithParentKeyColumn("UserId")
       .WithChildKeyColumn("AdvanceMessageId")
       .Inverse().AsSet();

这将在使用两列的链接表上设置一个唯一的复合主键约束。
(聚集索引)

缺点是 AsSet() 不能与 IList 类型的集合属性一起使用,因此不进行强制转换就没有 for 循环。

我一直在使用 ICollection 并将它们实例化为我的应用程序的 HashSet,并且效果很好。


有关使用 Fluent Nhibernate 进行集合管理的更多信息:

列表:实体的有序集合,允许重复。在代码中使用 .net IList。索引列需要在 NHibernate 中进行映射。

Set:唯一实体的无序集合,不允许重复。在代码中使用 Iesi.Collection.ISet。重写 GetHashCode 和 Equals 以指示重复的业务定义非常重要。可以通过定义 orderby 或定义比较器来排序,从而产生 SortedSet 结果。

Bag:实体的无序列表,允许重复。在代码中使用 .net IList。列表的索引列未映射,且不受 NHibernate 认可。

Old post... but in case someone else arrives here looking for the answer:

You need to add .AsSet() to the HasManyToMany mapping definintion.


i.e.

mapping.HasManyToMany(x => x.Users)
       .WithTableName("MessageReceivers")
       .WithParentKeyColumn("UserId")
       .WithChildKeyColumn("AdvanceMessageId")
       .Inverse().AsSet();

This will setup an unique, composite primary key constraint on the link table that uses both columns.
(clustered index)

The down side is AsSet() cannont be used with collection properties of type IList, so no for loops without casting.

I have been using ICollection and instantiating them as HashSet for my applications and it works well.


More info on collection management with Fluent Nhibernate:

List: Ordered collection of entities, duplicate allowed. Use a .net IList in code. The index column will need to be mapped in NHibernate.

Set: Unordered collection of unique entities, duplicates not allowed. Use Iesi.Collection.ISet in code. It is important to override GetHashCode and Equals to indicate the business definition of duplicate. Can be sorted by defining a orderby or by defining a comparer resulting in a SortedSet result.

Bag: Unordered list of entities, duplicates allowed. Use a .net IList in code. The index column of the list is not mapped and not honored by NHibernate.

呆橘 2024-08-18 03:19:33

您还应该映射关系的反面,例如

mapping.HasManyToMany(x => x.Users)
       .WithTableName("MessageReceivers")
       .WithParentKeyColumn("UserId")
       .WithChildKeyColumn("AdvanceMessageId")
       .Inverse();

在最新的 Fluent NHibernate 中,您必须更改

  • WithTableName ->表格

  • WithParentKeyColumn -> ParentKeyColumn

  • WithChildKeyColumn -> 父键列

    WithChildKeyColumn -> ChildKeyColumn

You should also map the inverse side of the relationship like

mapping.HasManyToMany(x => x.Users)
       .WithTableName("MessageReceivers")
       .WithParentKeyColumn("UserId")
       .WithChildKeyColumn("AdvanceMessageId")
       .Inverse();

In newest Fluent NHibernate you will have to change

  • WithTableName -> Table

  • WithParentKeyColumn -> ParentKeyColumn

  • WithChildKeyColumn -> ChildKeyColumn

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