How to map IDictionary<SomeEntity, int> in fluent?

38 views
Skip to first unread message

Fost andy

unread,
Aug 30, 2009, 2:16:47 PM8/30/09
to Fluent NHibernate
Given an IDictionary<SomeEntity, int> Foo; I'm basically trying to
produce this hbm.xml in fluent:

<map name="Foo">
<key column="..." />
<index-many-to-many class="SomeEntity" column="SomeEntity_Id"/>
<element column="Value" type="int"/>
</map>

After some experimentation I seem to be able to produce mappings for
an IDictionary<SomeEntity, SomeEntity>:

// produce a <map> with <index-many-to-many> and <many-to-many>,
HasManyToMany(x => x.Foo)
.AsMap
.AsTernaryAssociation("Key2", "Value")
;

And for an IDictionary<int, int>:

// produce a <map> with <index> and <element>HasMany(x => x.Foo)
.AsMap<int>
.Element("Value")
;

I even seem to be able to use AsIndexedCollection to produce <map>
with <index> and <many-to-many> (although none that NHibernate will
accept)

But I can't figure out how to produce a <map> with <index-many-to-
many> and <element>. Can someone provide some tips?

Fost andy

unread,
Aug 30, 2009, 2:18:10 PM8/30/09
to Fluent NHibernate
I forgot to mention, this is with 1.0 RTM

Fost andy

unread,
Sep 1, 2009, 3:19:06 AM9/1/09
to Fluent NHibernate
Sorry folks, I did not realize this got a bit confounded in
formatting.

The actual fluent nhibernate code and corresponding generated hbm.xml
code is below.

For an IDictionary<int,int>:

HasMany(x => x.IntDict)
.AsMap<int>("Key")
.Element("Value")
;

<map name="IntDict">
<key>
<column name="Node_id" />
</key>
<index type="System.Int32, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Key" />
</index>
<element type="System.Int32, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Value" />
</element>
</map>

And for an IDictionary<SomeEntity,SomeEntity>:

HasManyToMany(x => x.EntityDict)
.AsMap("Key")
.AsTernaryAssociation("Key", "Value")
;

<map name="EntityDict">
<key>
<column name="Node_id" />
</key>
<index-many-to-many class="ClassLibrary1.TernaryJoy.SomeEntity,
ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="Key" />
</index-many-to-many>
<many-to-many class="ClassLibrary1.TernaryJoy.SomeEntity,
ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="Value" />
</many-to-many>
</map>


And an IDictionary<SomeEntity,int>:

// the fluent code remains a mystery... anybody?

<map name="HybridDict">
<key column="Entity_Id" />
<index-many-to-many class="SomeEntity" column="SomeEntity_Id"/
>
<element column="Value" type="int"/>
</map>

Reply all
Reply to author
Forward
0 new messages