Automappings and table-per-class-hierarchy

264 views
Skip to first unread message

kberridge

unread,
Mar 9, 2010, 5:37:47 PM3/9/10
to Fluent NHibernate
Hi,
I'm looking for an example of how to use a table-per-class-hierarchies
with Automapper.

I've setup my automappings so the default Subclass Strategy is
Subclass:
autoMappings.Setup( s => s.SubclassStrategy = t =>
SubclassStrategy.Subclass );

I've included the parent of the class Hierarchy with:
autoMappings.IncludeBase<Parent>();

I have also defined an IAutoMappingOverride for the Parent:
public class ParentOverrideMap : IAutoMappingOverride<Parent>
{
public void
Override( FluentNHibernate.Automapping.AutoMapping<Parent> mapping )
{
mapping.DiscriminateSubClassesOnColumn( "Type" );
mapping.SubClass<ChildA>( "ChildA" );
}
}

From what I've read on the Wiki on in various Google searches it seems
like that should work, but when I test it by using the SchemaExport
tool, it builds the table with the discriminator column name as
"discriminator" instead of "Type". So it seems my overrides aren't
working.

The documentation on the Wiki doesn't cover this, so I've mostly been
guessing, that's why I'm wondering if anyone could point me to an
article somewhere with a good example of using the table-per-class-
hierarchy strategy with automappings.

Thanks,
Kevin

kberridge

unread,
Mar 22, 2010, 2:06:39 PM3/22/10
to Fluent NHibernate
Last check, can anyone provide any direction for me on this?

Thanks,
Kevin

Felipe Leusin

unread,
Apr 12, 2010, 12:46:06 AM4/12/10
to Fluent NHibernate
Same problem here... probably gonna simply disable AutoMapping for
those classes.

On Mar 22, 3:06 pm, kberridge <kevin.w.berri...@gmail.com> wrote:
> Last check, can anyone provide any direction for me on this?
>
> Thanks,
> Kevin
>
> On Mar 9, 6:37 pm, kberridge <kevin.w.berri...@gmail.com> wrote:
>
> > Hi,
> > I'm looking for an example of how to use a table-per-class-hierarchies
> > with Automapper.
>
> > I've setup my automappings so the default Subclass Strategy is
> > Subclass:
> > autoMappings.Setup( s => s.SubclassStrategy = t =>
> > SubclassStrategy.Subclass );
>
> > I've included the parent of the class Hierarchy with:
> > autoMappings.IncludeBase<Parent>();
>
> > I have also defined an IAutoMappingOverride for the Parent:
> > public class ParentOverrideMap : IAutoMappingOverride<Parent>
> > {
> >   public void
> > Override( FluentNHibernate.Automapping.AutoMapping<Parent> mapping )
> >   {
> >     mapping.DiscriminateSubClassesOnColumn( "Type" );
> >     mapping.SubClass<ChildA>( "ChildA" );
> >   }
>
> > }
>
> > From what I've read on the Wiki on in various Google searches it seems
> > like that should work, but when I test it by using the SchemaExport

> > tool, it builds the table with thediscriminatorcolumn name as

reach4thelasers

unread,
Apr 10, 2010, 12:14:11 PM4/10/10
to Fluent NHibernate
I've been searching for a solution to this all day. Does anyone have
a solution?

Felipe Leusin

unread,
Apr 14, 2010, 7:42:04 AM4/14/10
to Fluent NHibernate
A follow-up: If u set the DiscriminatorColumn in the setup part of
AutoMapper you can change the name of the column, ie:

DiscriminatorColumn = type => (type == typeof(MyClass)) ?
"IsHomeTeam" : "discriminator";

But I've been having no luck in determining a way to set
DiscriminatorColumn type and removed the classes altogether from
AutoMapping (btw, we should really make this easier, the ignoring from
automapper part).

Best of luck,
Felipe

On Apr 10, 1:14 pm, reach4thelasers <kev.m.mul...@googlemail.com>
wrote:

Berryl Hesh

unread,
Apr 14, 2010, 3:04:08 PM4/14/10
to Fluent NHibernate
I find the same behavior too - I can't set the column name of the
discriminator and I can't override the default behavior of using the
fully qualified type as the discriminator value.

I posted a question here a few weeks ago looking for a way to see if
there was a convention I could use and how to use it but no responses
at all, and definitely not obviously documented. I do get the
inheritance behavior at least! Life in the open source lane ...

Cheers,
Berryl

Felipe Leusin

unread,
Apr 15, 2010, 7:59:43 AM4/15/10
to Fluent NHibernate
Just a heads up, after some further digging up I found some
inconsistencies in AutoMapper.cs method MapInheritanceTree,
there is no check to see if mappings.Discriminator is set whatsoever.

Now, if I add something simple as:

if (((ClassMapping)mapping).Discriminator != null) {
discriminatorSet = true;
}

Then it works. And I can even change the discriminator using an
Override, but I've yet to find a way to create a Subclass Override. If
I can find one, then I'm gold.

Anyway, I'll do some further working on this and let you guys know of
any updates.

Cya,
Felipe

Felipe Leusin

unread,
Apr 15, 2010, 8:09:53 AM4/15/10
to Fluent NHibernate
Just one more quick add-in, the code I changed is this
(AutoMapper.cs : Lin 71):

if (isDiscriminated && !discriminatorSet && mapping is ClassMapping)
{


if (((ClassMapping)mapping).Discriminator != null)
{
discriminatorSet = true;
}

else {
var discriminatorColumn =
expressions.DiscriminatorColumn(classType);
var discriminator = new DiscriminatorMapping {
ContainingEntityType = classType,
Type = new TypeReference(typeof(string))
};
discriminator.AddDefaultColumn(new
ColumnMapping { Name = discriminatorColumn });

((ClassMapping)mapping).Discriminator =
discriminator;
discriminatorSet = true;
}
}

Now is off to figure out a way to make an Override for Subclass OR
create a new Convention perhaps to set DiscriminatorValue for the
subclasses.

Berryl Hesh

unread,
Apr 16, 2010, 1:44:31 PM4/16/10
to Fluent NHibernate
Keep up the nice work Felipe. Thanks!
--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply all
Reply to author
Forward
0 new messages