Monday, April 25, 2011

Fluent NHibernate HasManyToMany and NotFound

I have the following FNH mapping fragment:

HasManyToMany((c) => c.Plaintiffs)

Which produces the following HBM:

<bag name="Plaintiffs" access="iServe.Design.CslaNHibernate.CslaChildPropertyAccessor, iServe, Version=, Culture=neutral, PublicKeyToken=null" lazy="true" table="invoicePlantiff">
  <key column="invoiceID" />
  <many-to-many column="PersonReferenceID" class="iServe.PersonReference, iServe, Version=, Culture=neutral, PublicKeyToken=null" fetch="join" />

The problem is that I am dealing with legacy data that is, let us just say 'less than ideal', and some records have invalid ids so I need to put the not-found="ignore" attribute on the many-to-many tag inside the bag. I do not see ANY way to accomplish this with FNH.

There is the .NotFound.Ignore() on the References() call, but not on the HasManyToMany Call.

Can someone tell me what I am missing? Even if it is a hack to 'inject' the attribute after it is rendered / before configuring NH is fine, i just need it to work.

Thanks in advance.

UPDATE gcores answer will not work, it adds it to the wrong tag, see my comment on his answer for more info.

From stackoverflow
  • I'm not sure about this but isn't there a SetAttribute?

    HasManyToMany((c) => c.Plaintiffs)
    .SetAttributte("not-found", "ignore");


    Sorry about that, I knew there was a SetAttribute but I wasn't sure it did what you wanted.

    Another option would be to mix Fluent NH and XML mappings, the Fluent NH wiki explains how to do this or could see this answer from stackoverflow.

    Andrew Burns : Actually gcores that does nto work because it added the attribute to the `bag` and that is invalid. It needs to go on the `many-to-many` inside the bag.
  • I've just committed a change that adds NotFound to the HasMany and HasManyToMany calls. Hope it helps!


Post a Comment