Get table names from Fluent Nhibernate

After you have configured your mappings in free nhibernate, is there a way to get the table name for an object from a class type?

I read in the usual nhiberante, you can do something like cfg.GetClassMapping(typeof (Employee)) . I would like to do a type of thing to get the database table name.

Is this possible as standard or how can I do this?

+6
fluent-nhibernate
source share
4 answers

Nhibernate free path:

 var userMetadata = sessionFactory.GetClassMetadata(typeof(SomeEntity)) as NHibernate.Persister.Entity.AbstractEntityPersister; var cols = userMetadata.KeyColumnNames; var table = userMetadata.TableName; 

where sessionFactory is of type ISessionFactory.

+10
source share

Assuming you did this at some point:

 FluentNHibernate.Cfg.FluentConfiguration fluentConfig = FluentNHibernate.Cfg.Fluently.Configure(); 

Then all you have to do is the following:

 string tableName = fluentConfig.BuildConfiguration().GetClassMapping(typeof (One of your data entities)).Table.Name; 

Works great in my implementation of the GetAllItems universal procedure.

+2
source share

Hi, I use this to create full-text directories on the M $ Sql ​​server using almost the same FluentNhibernate mapping mechanism.

From the configuration, I get a list of persistentClasses

 this.persistenClasses = configuration.ClassMappings; 

Next, I go through this list to find my persistenClass class by its generic class class

 var genericDefinition = mappingClass.BaseType.GetGenericArguments()[0]; var matchedPersistClass = FindPersistedClassFrom(genericDefinition); private PersistentClass FindPersistedClassFrom(Type genericDefinition) { return persistentClasses.FirstOrDefault(x => x.EntityName == genericDefinition.FullName); } 

Thus, having persistentClass, you easily get access to table names, properties, db fields, etc.

 TableName = matchedPersistClass.Table.Name, 
0
source share

cfg.GetClassMapping(typeof(Employee)).Table.Name will work as well and seems a lot easier.

0
source share

All Articles