NHibernate ClassMap get column name for property

后端 未结 2 1095
日久生厌
日久生厌 2021-01-21 10:16

I have ClassMap and entity. In code I would like to get column names for properties in entity. Any Idea?

public class AccountToDepotMap : ClassMap

        
相关标签:
2条回答
  • 2021-01-21 10:41

    There is an deep overview:

    Exploring nhibernate mapping

    And the simple code snippet from here

    How do you get the database column name out of an auditing event listener in NHibernate

    how to get the a persister:

    var entityType = typeof(TEntity);
    var factory = ... // Get your ISessionFactory
    var persister = factory.GetClassMetadata(entityType) as AbstractEntityPersister;
    

    which can be later iterated

    // the set of Properties
    var propertyNameList = persister.PropertyNames;
    
    // here we get all the settings for remaining mapped properties
    foreach (var propertyName in propertyNameList)
    {
        ...
    
    0 讨论(0)
  • 2021-01-21 10:42

    using above code

    This is working in my case

    List<string> columns = new List<string>();
            for (int i = 0; i < persister.PropertyNames.Count(); i++)
            {
                if (persister.GetPropertyColumnNames(i).Count() > 0)
                {
                    columns.Add(persister.GetPropertyColumnNames(i).ToList().First());
                }
            }
    
    0 讨论(0)
提交回复
热议问题