NHibernate ClassMap 获取 属性 的列名

NHibernate ClassMap get column name for property

我有 ClassMap 和实体。在代码中,我想获取实体中属性的列名。有什么想法吗?

public class AccountToDepotMap : ClassMap<AccountToDepot>
{
    public AccountToDepotMap()
    {
        Table("COPS_WPA_ACCOUNT_DEPOT");
        Id(t => t.RecId, "REC_ID");
        Map(t => t.RecordDate, "REC_DATE");
        Map(t => t.DepotId, "WPA_DEPOT_ID");
        Map(t => t.AccountId, "WPA_ACCOUNT_ID");
        Map(t => t.IsActive, "ISACTIVE").CustomType<YesNoType>();
    }
}

public class AccountToDepot
{
    public virtual long RecId { get; set; }
    public virtual DateTime RecordDate { get; set; }
    public virtual string DepotId { get; set; }
    public virtual string AccountId { get; set; }
    public virtual bool IsActive { get; set; }
}

我想在 属性 DepotId 上提问,结果将是 WPA_DEPOT_ID

有深度概览:

Exploring nhibernate mapping

以及此处的简单代码片段

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

如何获得持久性:

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

以后可以迭代

// the set of Properties
var propertyNameList = persister.PropertyNames;

// here we get all the settings for remaining mapped properties
foreach (var propertyName in propertyNameList)
{
    ...

使用上面的代码

这适用于我的情况

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());
            }
        }