On Sun, Nov 9, 2014 at 1:10 AM, Michael Powell <mwpowell...@gmail.com> wrote:
> On Sun, Nov 9, 2014 at 12:27 AM, Michael Powell <mwpowell...@gmail.com> wrote:
>> Hello,
>>
>> I've managed to build up a series of Collection/CollectionItem
>> containers, whose Items themselves host another domain model,
>> something like CollectionItem<Payload> type thing.
>>
>> For the most part I've been successful in mapping these to the
>> database through Fluent NHibernate, but I am running into an odd
>> corner case in the ForeignKeyConvention.
>>
>> I do the sort of prototypical:
>>
>>         protected override string GetKeyName(Member property, Type type)
>>         {
>>             if (property != null)
>>                 return property.Name + @"Id";
>>
>>             return type.Name + @"Id";
>>         }
>>
>> if you will.
>>
>> However, this isn't quite right in all cases, and I do indeed want to
>> depend on Member property being there as this contains the property
>> name of concern.

Thank ye SO...

http://stackoverflow.com/questions/5967717/fluent-nhibernate-hasmany-foreign-key-mapping-problem

I do have the convention. I added the KeyColumns like was suggested
and that fixed the issue.

Methinks that does not need to happen in every case, excepting for
concerns like mapping a heavily generic class hierarchy.

>> The type name is incorrect in virtually all cases.
>>
>> The corner case is that property is turning up null in the
>> Collection/CollectionItem use case. Note, it is heavy in the generics,
>> is really unavoidable.
>>
>> Is there a better way to utilize conventions to do this? And/or
>> otherwise workaround the convention, such as to specify the KeyName at
>> the time of mapping?
>>
>> It seems I've tried this too, but the convention wants to operate
>> instead. That is unless there's something else I can override in the
>> convention to flag this being the case.
>
> The following Sql is where it falls over. All but the
> CharacteristicQuantityCollectionId are correct I believe. I have no
> idea where that column is being discovered.
>
> exec sp_executesql N'SELECT items0_.CharacteristicQuantityCollectionId
> as Characte6_1_, items0_.Id as Id1_, items0_.Id as Id1_0_,
> items0_.CollectionId as Collecti2_1_0_, items0_.QuantityId as
> QuantityId1_0_, items0_.Value as Value1_0_, items0_.Dimension as
> Dimension1_0_ FROM CharacteristicQuantityCollectionItems items0_ WHERE
> items0_.CharacteristicQuantityCollectionId=@p0',N'@p0
> uniqueidentifier',@p0='00598B6F-11E2-4743-9159-A3DE00111E8E'
>
> I'm not sure how that's not working, in spite of the fact I've got the
> referencing column named explicitly. The only thing I can think of is
> that some sort of generic issue is manifesting. That, and what can I
> do to more explicitly control the behavior, and/or conventionally do
> so.
>
> This is the Fluent for the parent side:
>
>             HasMany(x => x.Items)
>                 .Inverse()
>                 .Cascade.AllDeleteOrphan();
>
> And for the child side:
>
>             References(x => x.Collection, @"CollectionId")
>                 .Not.Nullable();
>
> And the generated Hbm, which does actually show the offending column name.
>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
>   <class xmlns="urn:nhibernate-mapping-2.2"
> name="Football.Mechanics.Dimensions.CharacteristicQuantityCollection,
> Football, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
> table="CharacteristicQuantityCollections">
>     <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0,
> Culture=neutral, PublicKeyToken=b77a5c561934e089">
>       <column name="Id" not-null="true" />
>       <generator class="guid.comb" />
>     </id>
>     <bag access="field.camelcase-underscore"
> cascade="all-delete-orphan" inverse="true" name="Items">
>       <key>
>         <column name="CharacteristicQuantityCollectionId" />
>       </key>
>       <one-to-many
> class="Football.Mechanics.Dimensions.CharacteristicQuantityCollectionItem,
> Football, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
>     </bag>
>     <many-to-one class="Football.Models.Player, Football,
> Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Player">
>       <column name="PlayerId" not-null="true" />
>     </many-to-one>
>   </class>
> </hibernate-mapping>
>
>
>> Thank you...
>>
>> Best regards,
>>
>> Michael Powell

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fluent-nhibernate+unsubscr...@googlegroups.com.
To post to this group, send email to fluent-nhibernate@googlegroups.com.
Visit this group at http://groups.google.com/group/fluent-nhibernate.
For more options, visit https://groups.google.com/d/optout.

Reply via email to