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-convention.html


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
>    }
>
>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to