NHibernate Forge
The official new home for the NHibernate for .NET community

Automatic Mapping: Pluralize table names

Note: this is a cross post from my own blog.

In this post we done some effort in automatically generate the mapping based on convention, but we miss a very common one: table names is usually the pluralized entity name. This is usually done by using an inflector. Thanks to Stack Overflow, I found this question about it, and choose that one, that is a single easily embeddable file. So we modify a little our AutoMapper class as below:

void AutoMapper_BeforeMapClass(IModelInspector modelInspector, Type type, IClassAttributesMapper classCustomizer)
           // Create the column name as "c"+EntityName+"Id"
           classCustomizer.Id(k => 
                                   k.Generator(Generators.Native); k.Column("c" + type.Name + "Id"); 


And this is all, the generated mapping will change as:

<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x=""
sd="http://www.w3.org/2001/XMLSchema" namespace="MappingByCode" assembly="Mappin
gByCode" xmlns="urn:nhibernate-mapping-2.2">
  <class name="SimpleEntity" table="SimpleEntities">
    <id name="Id" column="cSimpleEntityId" type="Int32">
      <generator class="native" />
    <property name="Description">
      <column name="txtSimpleEntityDescr" sql-type="AnsiString" />
    <many-to-one name="Referred" column="cReferredId" />

Just for better sharing, I published this “laboratory” project here.

Posted sep 05 2011, 08:28 a.m. by felicepollano
Filed under: ,


NHibernate blog wrote NH Mapping by code VS the Untouchable DB
on 09-12-2011 5:32

Note: this is a cross post from my own blog . This post is an exercise, similar to this and this previous

شارژ مستقیم همراه اول wrote شارژ مستقیم همراه اول
on 11-22-2014 23:10

Automatic Mapping: Pluralize table names - NHibernate blog - NHibernate Forge

Powered by Community Server (Commercial Edition), by Telligent Systems