As soon as the session factory is built, you cannot change it. For that, 
you need to change the configuration and build a new session factory from 
it.
Here's an example on how to change the table name, doesn't matter what you 
are using for the mappings (.hbm.xml, fluent, etc):

cfg.GetClassMapping(typeof(MyClass)).Table = "MY_TABLE";


RP

On Sunday, September 15, 2013 9:06:15 AM UTC+1, Atilla İlhan KARTAL wrote:
>
> Dear All;
>
> I want to change Maping Schema and Catalog name in runtime;
>
> I tried this code snipped. This code snipped can catalog name change for 
> Select Query but dont change insert query.
>
> I want to use multiple database in my project. Select Queries is OK but 
> Insert Queries is NO.
>
>
> var config = CurrentConfiguration.BuildConfiguration();
>             var schemaName = GenerateSchemaName(schema);
>             var catalogName = GenerateCatalogName(schema);
>             foreach (var source in
>                     config.ClassMappings.Where(
>                         x => 
> x.MappedClass.GetCustomAttributes(typeof(SessionContext), true) != null))
>             {
>                 SessionContext sessionContext =
>                     
> source.MappedClass.GetCustomAttributes(typeof(SessionContext), 
> true).First() as SessionContext;
>                 
>                 if (sessionContext != null)
>                 {
>                     if (sessionContext.Name.Equals(contextName))
>                     {
>                         source.Table.Schema = schemaName;
>                         source.Table.Catalog = catalogName;
>                         source.IdentityTable.Schema = schemaName;
>                         source.IdentityTable.Catalog = catalogName;
>                         source.RootTable.Schema = schemaName;
>                         source.RootTable.Catalog = catalogName;
>                         
> Console.WriteLine(String.Format("{0}->{1}",source.EntityName,source.Table.Schema));
>                     }
>                 }
>             }
>             foreach (
>                 var source in
>                     config.CollectionMappings.Where(x => !x.IsOneToMany))
>             {
>                 if (source.Owner != null)
>                 {
>                     SessionContext sessionContext =
>                     
> source.Owner.MappedClass.GetCustomAttributes(typeof(SessionContext), 
> true).FirstOrNull() as SessionContext;
>                     if (sessionContext != null)
>                     {
>                         if (sessionContext.Name.Equals(contextName))
>                         {
>                             source.Table.Schema = schemaName;
>                             source.Table.Catalog = catalogName;
>                         }
>                     }
>                 }
>             }
>
>
>
> I tried change in ClassMap with this code snipped. Insert statement use 
>  ChangeDB.dbo.ACCOUNTING_TB_VATRATE table part.
>
>
>
> public class VatRateMap : ClassMap<VatRate>
>         {
>             public VatRateMap()
>             {
>                 Table("ACCOUNTING_TB_VATRATE");
>                 
>                 this.HibernateMapping.Catalog("ChangeDB");
>                 this.HibernateMapping.Schema("dbo");
>                 
>                 SchemaAction.None();
>                 Id(x => x.ID, "ID").GeneratedBy.Identity();
>                 Map(x => x.Label, "Label");
>                 Map(x => x.Rate, "Rate");
>
>             }
>         }
>
>
> How can i change this table name in runtime?
>
> -- 
> Atilla İlhan KARTAL
> Web Application & Software Architect
> (Java & PHP & Registered Android Developer)
> Kuşadası / Aydın / Turkey
> www.atillailhankartal.com.tr
> twitter.com/TrojanMyth
>  

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to