流利的NHibernate映射的双向,多对多关联使用关联类 [英] Fluent NHibernate mapping of a bi-directional, many-to-many association using an association class

查看:151
本文介绍了流利的NHibernate映射的双向,多对多关联使用关联类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在EntityA和EntityB之间有一个双向的多对多关联,我使用一个关联类EntityABLink来建模,因为还有其他的关于我需要跟踪的关系的属性。另外,我还有另一个类,它提到EntityA和EntityB之间的特定关系,所以我把这个关联类当成一个完整的实体。



在EntityA中,我有一个只读属性,返回一个关联的EntityB对象的列表,同样,在EntityB中,我有一个只读属性,返回一个列表关联的EntityA对象。请注意,对于这些属性,我隐藏了关联通过关联类实现的事实。 (我也有更新隐藏实现的关系的专用方法。)在EntityA和EntityB的幕后,我有EntityABLink类型的私有集合。

由于一张图片胜过千言万语,下面是我所描述的:

(请注意,EntityA和EntityB上的public,只读属性与支持它们的私人成员不同。)



到目前为止,这么好。现在我想用Fluent NHibernate自动映射覆盖将这些对象保存到数据库中。当涉及到映射时,我喜欢使用这个功能等价的表示法来思考上述问题:


从这张图中可以清楚地看到,真正需要的是两个双向的一对多关系。



在上面的映射中,我想我需要这样的东西:



在EntityA automapping覆盖:

  mapping.HasMany< EntityABLink>(Reveal.Member< EntityA>(_ AssociationList))。Inverse().AsBag ).Cascade.SaveUpdate(); 
mapping.IgnoreProperty(x => x.EntityBList);

在EntityB自动映射覆盖中:

  mapping.HasMany< EntityABLink>(Reveal.Member< EntityB>(_ AssociationList))。反()AsBag()Cascade.SaveUpdate(); 
mapping.IgnoreProperty(x => x.EntityAList);

在EntityABLink自动映射覆盖中:

  mapping.References< EntityA>(x => x.EntityA).Not.Nullable(); 
mapping.References< EntityB>(x => x.EntityB).Not.Nullable();

当我尝试这个时,我得到以下错误:

无法在类'EntityB'中找到属性'_ AssociationList'的getter。

我的映射必须有问题,但我不确定是什么。有任何想法吗?

解决方案

我现在正在工作。所以这里的技巧...我回到流利的NHibernate版本1.1(特别是1.1.0.685)。然后,虽然使用Reveal.Member的映射示例没有显示为必要,但我在EntityA._AssociationList和EntityB._AssociationList的映射中添加了Access.Field()。这里是工作映射。



在EntityA automapping覆盖中:

  mapping.HasMany< EntityABLink> ;(Reveal.Member< EntityA>(_ AssociationList))。反()AsBag()Cascade.SaveUpdate()Access.Field()。; 
mapping.IgnoreProperty(x => x.EntityBList);

在EntityB automapping覆盖中:

  mapping.HasMany< EntityABLink>(Reveal.Member< EntityB>(_ AssociationList))。反()AsBag()Cascade.SaveUpdate()Access.Field()。; 
mapping.IgnoreProperty(x => x.EntityAList);

在EntityABLink自动映射覆盖中:

  mapping.References< EntityA>(x => x.EntityA).Not.Nullable(); 
mapping.References< EntityB>(x => x.EntityB).Not.Nullable();

一旦它在FNH 1.1中工作,我尝试升级到FNH 1.2。 没有用。我试过1.2.0.694和1.2.0.712,这两个仍然给不正确的错误消息:不同的实体(实际上是一个枚举!)没有一个Id映射。

流利的NHibernate是一个很好的工具,所以我希望最新版本的bug能够得到修复。 :-)


I have a bi-directional, many-to-many association between EntityA and EntityB and I’m using an association class, EntityABLink, to model this because there are other attributes about the relationship that I need to track. In addition, I also have another class that holds a reference to a specific relationship between EntityA and EntityB so I treat the association class as a full-fledged entity.

In EntityA I have a read-only property that returns a list of associated EntityB objects and, likewise, in EntityB I have a read-only property that returns a list of associated EntityA objects. Note that, for these properties, I’m hiding the fact that the association is implemented via an association class. (I also have dedicated methods for updating the relationships that hide the implementation.) Behind the scenes in both EntityA and EntityB, I have private collections of type EntityABLink.

Since a picture is worth a thousand words, here is what I have described so far:

(Note again that the public, read-only properties on EntityA and EntityB are not of the same type as the private members that back them up.)

So far, so good. Now I want to persist these objects to a database using Fluent NHibernate automapping overrides. When it comes to mapping, I like to think of the above using this functionally equivalent representation:
From this diagram, it’s clear that what I really need is two bi-directional one-to-many relationships.

In mapping the above, I figure that I need something like this:

In the EntityA automapping override:

mapping.HasMany<EntityABLink>(Reveal.Member<EntityA>("_AssociationList")).Inverse().AsBag().Cascade.SaveUpdate(); 
mapping.IgnoreProperty(x => x.EntityBList);

In the EntityB automapping override:

mapping.HasMany<EntityABLink>(Reveal.Member<EntityB>("_AssociationList")).Inverse().AsBag().Cascade.SaveUpdate(); 
mapping.IgnoreProperty(x => x.EntityAList);

In the EntityABLink automapping override:

mapping.References<EntityA>(x => x.EntityA).Not.Nullable();
mapping.References<EntityB>(x => x.EntityB).Not.Nullable();

When I try this, however, I get the following error:

"Could not find a getter for property '_ AssociationList’ in class 'EntityB'."

I must have something wrong with my mappings, but I’m not sure what. Any ideas?

解决方案

I got it working now. So here's the trick... I reverted back to Fluent NHibernate version 1.1 (specifically 1.1.0.685). Then, although the mapping examples that use "Reveal.Member" don't show it as being necessary, I added "Access.Field()" to the mapping for both EntityA._AssociationList and EntityB._AssociationList. Here are the working mappings.

In the EntityA automapping override:

mapping.HasMany<EntityABLink>(Reveal.Member<EntityA>("_AssociationList")).Inverse().AsBag().Cascade.SaveUpdate().Access.Field(); 
mapping.IgnoreProperty(x => x.EntityBList);

In the EntityB automapping override:

mapping.HasMany<EntityABLink>(Reveal.Member<EntityB>("_AssociationList")).Inverse().AsBag().Cascade.SaveUpdate().Access.Field(); 
mapping.IgnoreProperty(x => x.EntityAList);

In the EntityABLink automapping override:

mapping.References<EntityA>(x => x.EntityA).Not.Nullable();
mapping.References<EntityB>(x => x.EntityB).Not.Nullable();

Once it was working in FNH 1.1, I tried upgrading to FNH 1.2. No good. I tried 1.2.0.694 as well as 1.2.0.712 and both of these still give the incorrect error message that a different "entity" (which is actually an enum!) doesn't have an Id mapped.

Fluent NHibernate is a wonderful tool so I hope that the bug in the latest version gets fixed. :-)

这篇关于流利的NHibernate映射的双向,多对多关联使用关联类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆