upgrading IHasManyToManyConvention to Fluent 1.0RC

84 views
Skip to first unread message

Chris Willard

unread,
Aug 26, 2009, 1:54:44 PM8/26/09
to Fluent NHibernate
Hello,

I am in the process of upgrading to the Fluent 1.0RC and and stuck on
my IHasManyToManyConvention convention. I can't seem to figure out
how to update ParentKeyColumn and ChildKeyColumn. Below is my
original version.

public void Apply(IManyToManyPart target)
{
if (target.TableName == null)
target.WithTableName(target.EntityType.Name +
target.ChildType.Name);
if (target.ParentKeyColumn == null)
target.WithParentKeyColumn(target.EntityType.Name +
"ID");
if (target.ChildKeyColumn == null)
target.WithChildKeyColumn(target.ChildType.Name +
"ID");
}

Thanks in advance,
Chris

James Gregory

unread,
Aug 27, 2009, 6:06:07 AM8/27/09
to fluent-n...@googlegroups.com
Firstly, be careful setting the many-to-many table name like you are, you can get into a mess like that. I'd recommend overriding the many-to-many table naming convention, which is specifically designed to handle both sides of a bi-directional many-to-many.

ParentKeyColumn == Key.Column
ChildKeyColumn == Relationship.Column

The latter is a little unclear I realise.

James Gregory

unread,
Aug 27, 2009, 6:07:23 AM8/27/09
to fluent-n...@googlegroups.com
Also, consider using the acceptance criteria instead of your null checks, as you can't guarantee something will be null as they get pre-populated with default values.

Chris W

unread,
Aug 27, 2009, 11:34:13 AM8/27/09
to Fluent NHibernate
Thanks James,

Yeah, I was stuck on the Key.Column and Relationship.Column thing.
Just to make sure I am understanding correctly, are you recommending
that I set the 2 key values via IHasManyToManyConvention but use
ManyToManyTableNameConvention for the table name? Also, do I need to
override both GetBiDirectionalTableName and
GetUniDirectionalTableName? Thanks again for the help!

Chris

On Aug 27, 4:07 am, James Gregory <jagregory....@gmail.com> wrote:
> Also, consider using the acceptance
> criteria<http://wiki.fluentnhibernate.org/Conventions#Conditional_applying_of_...>
> instead
> of your null checks, as you can't guarantee something will be null as they
> get pre-populated with default values.
>
> On Thu, Aug 27, 2009 at 11:06 AM, James Gregory <jagregory....@gmail.com>wrote:
>
> > Firstly, be careful setting the many-to-many table name like you are, you
> > can get into a mess like that. I'd recommend overriding the many-to-many
> > table naming convention<http://wiki.fluentnhibernate.org/Available_conventions#ManyToManyTabl...>,

James Gregory

unread,
Aug 28, 2009, 5:03:04 AM8/28/09
to fluent-n...@googlegroups.com
You're correct on both accounts.

You don't have to set the table name separately, but I would strongly recommend it. The base class that you derive from (ManyToManyTableNameConvention) has a lot of magic in for making sure it doesn't set the table name for both sides of a bi-directional (we used to have a bug where you'd end up with AToB and BToA for each side), and it's also careful not to override anything you've explicitly set. Like I said, you don't need to do this, but it can be a pain saver.

If you do go that route, you do need to implement both methods.

Chris W

unread,
Aug 28, 2009, 4:09:04 PM8/28/09
to Fluent NHibernate
Thank for the heads up! I went ahead and moved the table naming logic
to the base class you mentioned. Everything seems to be working well
now. I really appreciate all the help!

Thanks,
Chris

On Aug 28, 3:03 am, James Gregory <jagregory....@gmail.com> wrote:
> You're correct on both accounts.
> You don't have to set the table name separately, but I would strongly
> recommend it. The base class that you derive from
> (ManyToManyTableNameConvention) has a lot of magic in for making sure it
> doesn't set the table name for both sides of a bi-directional (we used to
> have a bug where you'd end up with AToB and BToA for each side), and it's
> also careful not to override anything you've explicitly set. Like I said,
> you don't need to do this, but it can be a pain saver.
>
> If you do go that route, you do need to implement both methods.
>

James Gregory

unread,
Aug 28, 2009, 8:02:46 PM8/28/09
to fluent-n...@googlegroups.com
Don't mention it :)
Reply all
Reply to author
Forward
0 new messages