NHibernate ClassMap get column name for property

半城伤御伤魂 提交于 2019-12-20 03:18:04

问题


I have ClassMap and entity. 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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/33418454/nhibernate-classmap-get-column-name-for-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!