简体   繁体   English

NHibernate ClassMap获取属性的列名

[英]NHibernate ClassMap get column name for property

I have ClassMap and entity. 我有ClassMap和实体。 In code I would like to get column names for properties in entity. 在代码中,我想获取实体属性的列名。 Any Idea? 任何想法?

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

I would like to ask on property DepotId and result will be WPA_DEPOT_ID . 我想问一下属性DepotId ,结果将是WPA_DEPOT_ID

There is an deep overview: 有一个深入的概述:

Exploring nhibernate mapping 探索Nhibernate映射

And the simple code snippet from here 还有这里的简单代码段

How do you get the database column name out of an auditing event listener in NHibernate 如何从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)
{
    ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM