views:

41

answers:

1

Question: I have the below nHibernate mapping via attributes.

Now I want to create T_lsDomains with a dynamic table prefix. Such as TBL_lsDomains or just lsDomains.

Is there any way I can do this with attributes? Since they are defined at compile time, and must be constant?

Is there any way to do this?

Or can fluentNhibernate do this ?

using System;
using System.Collections.Generic;
using System.Text;

namespace nhDBapi.Tables
{

    [NHibernate.Mapping.Attributes.Class(Name = "nhDBapi.Tables.clsDomains, nhDBapi", Table = "T_lsDomains")]
    public class clsDomains
    {
        void clsDOmains()
        { 
        }


        [NHibernate.Mapping.Attributes.Id(Name = "DomainID", Column = "DM_DomainID", TypeType = typeof(string))]
        public string DomainID = "abc"; // nvarchar(100) NULL DEFAULT (''),

        [NHibernate.Mapping.Attributes.Property(Name = "DomainName", Column = "DM_DomainName", Type = "String", Length = 100)]
        string DomainName = ""; // nvarchar(100) NULL DEFAULT (''),

        [NHibernate.Mapping.Attributes.Property(Name = "Description", Column = "DM_Description", Type = "String", Length = 100)]
        string Description = ""; // nvarchar(100) NULL DEFAULT (''),
    }
}
+2  A: 

You can easily achieve this using a Fluent NHibernate convention.:

public class TableNameConvention : IClassConvention
{
  public bool Accept(IClassMap classMap)
  {
    return true; // apply to all mappings
  }

  public void Apply(IClassMap classMap)
  {
    // will produce table names like: TBL_Customer, TBL_Product
    classMap.WithTable("TBL_" + classMap.EntityType.Name);
  }
}
Rafael Belliard