NHibernate ClassMap获取属性的列名

时间:2015-10-29 15:32:43

标签: c# nhibernate nhibernate-mapping

我有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

2 个答案:

答案 0 :(得分:4)

有一个深刻的概述:

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)
{
    ...

答案 1 :(得分:2)

使用上面的代码

这适用于我的情况

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