It turns out the answer is to set the desired value by calling
WithTable in the Apply method of the custom coded convention class.
My code for EntityTotBleNameMappingConvention ended up looking like
what you see below. My version of Hudson Akridge's pluralize logic
was factored out into a separate static class I created named
FluentNHibernateConventionHelpers with one static method called
PluralOf. The main change I made to my PluralOf method was to use
StringBuilder instead of "+=" to build up the plural return value.
using FluentNHibernate.Conventions;
using FluentNHibernate.Mapping;
namespace Mapper
{
/// <summary>
/// Override the Entity-to-Table Name mapping so that when we
"pluralize" entities
/// like Category we get a table named Categories, not Categorys.
/// </summary>
public class EntityToTableNameMappingConvention : IClassConvention
{
#region IConvention<IClassMap> Members
public bool Accept(IClassMap target)
{
// is it an entity in our entity namespace?
return target.EntityType.Namespace.EndsWith("Notes");
}
public void Apply(IClassMap target)
{
// use helper method to get the plural equivalent of the
entity
string pluralTableName =
FluentNHibernateConventionHelpers.PluralOf(target.EntityType.Name);
target.WithTable(pluralTableName);
//Debug.WriteLine(target.TableName);
}
#endregion
}
}
I used the above code while Fluently configuring NHibernate as
follows:
ISessionFactory returnValue = null;
// Use Fluent NHibernate instead of an NHibernate XML
config file
returnValue = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c =>
c.FromConnectionStringWithKey("ConnectionString") )
.ShowSql()
.DefaultSchema("dbo")
.ProxyFactoryFactory
("NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle")
)
// Automap the POCO's from class Note plus anything
else that satisfies all these conditions:
// 1. has the correct namespace
specification
// 2. is a concrete class.
.Mappings(m => m.AutoMappings
.Add
(AutoPersistenceModel.MapEntitiesFromAssemblyOf<Note>()
.Where(entity =>
entity.Namespace.EndsWith
("Notes")
&& entity.IsAbstract ==
false
) // Where
// special conventions can be added here as
well:
.ConventionDiscovery
.Setup ( s =>
{
// start with some built-in short-cut
type conventions:
// The primary key attribute of each
entity is always called Id
s.Add(PrimaryKey.Name.Is(p => "Id"));
// always default to lazy loading
s.Add(DefaultLazy.AlwaysTrue());
// throw in some custom conventions
that require that we write some code
s.Add<DefaultStringLengthConvention>
();
s.Add<PassphraseLengthConvention>();
// this one helps make sure the table
name is the plural form of the entity
s.Add<EntityToTableNameMappingConvention>();
}
) // Setup
//
ConventionDiscovery
) // Add
) // Mappings
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory()
;
return returnValue;
On Apr 28, 6:46 pm, Hudson Akridge <[email protected]> wrote:
> I had this blog article post dated, but I went ahead and published it early
> for you, rather than reiterating what I already typed up ;) So this post
> should be everything you need
> here.http://www.bestguesstheory.com/2009/04/fluent-nhibernate-tablename-co...
>
> I have all of my conventions using those methods, with satisfying results.
> Let me know on here, or the blog if you've got any questions!
>
>
>
> On Tue, Apr 28, 2009 at 4:05 PM, fmorriso <[email protected]> wrote:
>
> > I want to make sure that the database table name for entity Employee
> > is Employees, but the table name for entity Category should be
> > Categories, not Categorys.
>
> > The short-cut method shown below doesn't work, but the custom code
> > technique is a big mystery at the moment, so I just need a hint or
> > two.
>
> > Currently, the one-size-fits-all approach is this:
>
> > returnValue = Fluently.Configure()
> > .Database(MsSqlConfiguration.MsSql2008
> > .ConnectionString(c =>
> > c.FromConnectionStringWithKey("ConnectionString") )
> > .ShowSql()
> > .DefaultSchema("dbo")
> > .ProxyFactoryFactory
> > ("NHibernate.ByteCode.Castle.ProxyFactoryFactory,
> > NHibernate.ByteCode.Castle")
> > )
> > // Automap the POCO's from class Note plus anything
> > else that satisfies all these conditions:
> > // 1. has the correct namespace
> > specification
> > // 2. is a concrete class.
> > .Mappings(m => m.AutoMappings
> > .Add
> > (AutoPersistenceModel.MapEntitiesFromAssemblyOf<Note>()
> > .Where(entity =>
> > entity.Namespace.EndsWith
> > ("Notes")
> > && entity.IsAbstract ==
> > false
> > ) // Where
> > // special conventions can be added here as
> > well:
> > .ConventionDiscovery
> > .Setup ( s =>
> > {
> > // start with some built-in short-cut
> > type conventions:
> > // database tables names are the
> > plural of their corresponding entity name
> > s.Add(Table.Is(t => t.EntityType.Name
> > + "s"));
> > // The primary key attribute of each
> > entity is called Id
> > s.Add(PrimaryKey.Name.Is(p => "Id"));
> > // always default to lazy loading
> > s.Add(DefaultLazy.AlwaysTrue());
> > // throw in some custom conventions
> > that require that we write some code
> > s.Add<DefaultStringLengthConvention>
> > ();
> > s.Add<PassphraseLengthConvention>();
> > }
> > ) // Setup
> > //
> > ConventionDiscovery
> > ) // Add
> > ) // Mappings
> > .ExposeConfiguration(BuildSchema)
> > .BuildSessionFactory()
> > ;
> > Ultimately, I'd like to do this instead:
>
> > returnValue = Fluently.Configure()
> > .Database(MsSqlConfiguration.MsSql2008
> > .ConnectionString(c =>
> > c.FromConnectionStringWithKey("ConnectionString") )
> > .ShowSql()
> > .DefaultSchema("dbo")
> > .ProxyFactoryFactory
> > ("NHibernate.ByteCode.Castle.ProxyFactoryFactory,
> > NHibernate.ByteCode.Castle")
> > )
> > // Automap the POCO's from class Note plus anything
> > else that satisfies all these conditions:
> > // 1. has the correct namespace
> > specification
> > // 2. is a concrete class.
> > .Mappings(m => m.AutoMappings
> > .Add
> > (AutoPersistenceModel.MapEntitiesFromAssemblyOf<Note>()
> > .Where(entity =>
> > entity.Namespace.EndsWith
> > ("Notes")
> > && entity.IsAbstract ==
> > false
> > ) // Where
> > // special conventions can be added here as
> > well:
> > .ConventionDiscovery
> > .Setup ( s =>
> > {
> > // start with some built-in short-cut
> > type conventions:
> > // database tables names are the
> > plural of their corresponding entity name
> > //s.Add(Table.Is(t =>
> > t.EntityType.Name + "s"));
> > // The primary key attribute of each
> > entity is called Id
> > s.Add(PrimaryKey.Name.Is(p => "Id"));
> > // always default to lazy loading
> > s.Add(DefaultLazy.AlwaysTrue());
> > // throw in some custom conventions
> > that require that we write some code
> > s.Add<DefaultStringLengthConvention>
> > ();
> > s.Add<PassphraseLengthConvention>();
> > s.Add<TableNameConvention>();
> > }
> > ) // Setup
> > //
> > ConventionDiscovery
> > ) // Add
> > ) // Mappings
> > .ExposeConfiguration(BuildSchema)
> > .BuildSessionFactory()
> > ;
>
> > where I'm "stuck" is how to write TableNameConvention, which currently
> > looks like this in its non-working state:
>
> > using FluentNHibernate.Conventions;
> > using FluentNHibernate.Mapping;
> > using System.Diagnostics;
>
> > namespace Mapper
> > {
> > /// <summary>
> > /// Override the Entity-to-Table Name mapping so that when we
> > "pluralize" entities
> > /// like Category we get a table named Categories, not Categorys.
> > /// </summary>
> > public class TableNameConvention : IClassConvention
> > {
> > #region IConvention<IClassMap> Members
>
> > public bool Accept(IClassMap target)
> > {
> > // is it an entity in our entity namespace?
> > return target.EntityType.Namespace.EndsWith("Notes");
> > }
>
> > public void Apply(IClassMap target)
> > {
> > //FAILS: target.SetAttribute("TableName",
> > target.EntityType.Name + "s");
> > Debug.WriteLine(target.TableName);
> > }
>
> > #endregion
> > }- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---