如何用NHibernate枚举列名?

时间:2009-04-15 21:09:02

标签: c# nhibernate inspection

我有一个带有一堆[ColumnName(“foo”)] NHibernate属性的类。 有没有一种简单的方法可以让NHibernate列出给定类的所有ColumnNames?

听起来应该很容易,但我只是没有在NHibernate文档中看到任何类型的检查(或者我今天只是失明)。

4 个答案:

答案 0 :(得分:7)

我遇到了同样的问题,但发现IClassMetadata没有任何列信息,只有属性类型,名称,标识符和表信息。

对我有用的是什么:

PersistentClass persistentClass = cfg.GetClassMapping(typeof(MyEntity));
Property property = persistentClass.GetProperty(propertyName);
property.ColumnIterator   // <-- the column(s) for the property

答案 1 :(得分:6)

如何获取NHibernate映射的实体的数据库列名:

using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Persister.Entity;

namespace Stackoverflow.Example
{
    /// <summary>
    /// NHibernate helper class
    /// </summary>
    /// <remarks>
    /// Assumes you are using NHibernate version 3.1.0.4000 or greater (Not tested on previous versions)
    /// </remarks>
    public class NHibernateHelper
    {
        /// <summary>
        /// Gets the list of database column names for an entity
        /// </summary>
        /// <param name="sessionFactory">NHibernate SessionFactory</param>
        /// <param name="entity">A mapped entity</param>
        /// <returns>List of column names</returns>
        public static IEnumerable<string> GetPropertyColumnNames(ISessionFactory sessionFactory, object entity)
        {
            Type entityType = entity == null ? null : entity.GetType();

            List<string> columnNameList = null;

            // This has some cool methods and properties so check it out
            var metaData = entityType == null ? null : sessionFactory.GetClassMetadata(entityType.ToString());

            //- metaData validity check ... will be null if provided type is not mapped
            if (metaData != null)
            {
                // This has some even cooler methods and properties so definitely check this out
                var entityPersister = (AbstractEntityPersister) metaData;

                //- how to get the entity's identifier
                //- string entityIdentifier = metaData.IdentifierPropertyName;

                //- Get the database identifier
                //- can have multiple in case of composite keys
                IEnumerable<string> dbIdentifierNameList = entityPersister.KeyColumnNames;

                var propertyNameList = entityPersister.PropertyNames;

                // Adding the database identifier first
                columnNameList = new List<string>(dbIdentifierNameList);
                //- then add properties column names
                foreach (var propertyName in propertyNameList)
                {
                    var columnNameArray = entityPersister.GetPropertyColumnNames(propertyName);
                    columnNameList.AddRange(columnNameArray.Where(columnName => dbIdentifierNameList.Contains(columnName) == false));
                }
            }

            return columnNameList;
        }
    }
}   

用法:

// Get your NHiberate SessionFactory wherever that is in your application
var sessionFactory = NHibernateHelper.SessionFactory;

// Get an entity that you know is mapped by NHibernate
var customer = new Customer();

// Get a list of the database column names for the entity
var columnNames = 
        Stackoverflow.Example.NHibernateHelper.GetPropertyColumnNames( sessionFactory, customer );

享受这种令人敬畏的荣耀:)

答案 2 :(得分:2)

使用LINQ和反射:

var columns = typeof(TheClass).GetProperties()
    .Where(property => property.GetCustomAttributes(typeof(ColumnNameAttribute), false).Count > 0)
    .Select(property => property.Name);

答案 3 :(得分:2)

使用NHibernate的元数据

// get an instance to the metadata 
IClassMetadata metadata = sessionfactory.GetClassMetadata(typeof(MyEntity));

// use properties and methods from the metadata:
// metadata.PropertyNames
// metadata.PropertyTypes
// metadata.GetIdentifier()
// and more

// or get the metadata for all classes at once
IDictionary allClassMetaData = factory.GetAllClassMetadata();
metadata = allClassMetaData[typeof(MyEntity)];

你得到NHibernate实际知道的东西,不管它是如何定义的;使用属性,xml映射或FluentNHibernate。这使得它比使用自己的反射更稳定,更可靠。