查找实体的属性映射到的数据库列

时间:2022-10-22 11:24:20

I was wondering if anyone knew how to find what column an entity's property is mapped to using NHibernate and only having the IEntityPersister interface available.

我想知道是否有人知道如何使用NHibernate找到实体的属性映射到的列,并且只有IEntityPersister接口可用。

2 个解决方案

#1


0  

You could parse the mapping file if you are using one. It's fairly simple xml so a simple xpath query would get you the column name. If you are using attributes you would have to use reflection to get the attribute from the property.

如果使用映射文件,则可以解析映射文件。它是相当简单的xml,因此一个简单的xpath查询可以获得列名。如果使用属性,则必须使用反射从属性中获取属性。

#2


0  

Here is some code that might help.

以下是一些可能有用的代码。

    public static string[] GetDatabaseColumnNamesFromEntityProperty(Type entityType, string propertyName)
    {
        PersistentClass aNHibernateClass = NHibernateSessionManager.Instance.GetNHibernateConfiguration().GetClassMapping(entityType);

        if (aNHibernateClass == null)
        {
            return null;
        }
        else
        {
            string[] columnNames = null;

            try
            {
                Property aProperty = aNHibernateClass.GetProperty(propertyName);
                columnNames = new string[aProperty.ColumnCollection.Count];

                int count = 0;

                foreach (Column column in aProperty.ColumnCollection)
                {
                    columnNames[count] = column.Name;
                    count++;
                }
            }
            catch(Exception)
            {
                Property aProperty = aNHibernateClass.IdentifierProperty;

                //if(aProperty.Name.Equals(propertyName))
                //{
                    columnNames = new string[aProperty.ColumnCollection.Count];
                    int count = 0;

                    foreach (Column column in aProperty.ColumnCollection)
                    {
                        columnNames[count] = column.Name;
                        count++;
                    }
                //}
            }

            return columnNames;
        }
    }

#1


0  

You could parse the mapping file if you are using one. It's fairly simple xml so a simple xpath query would get you the column name. If you are using attributes you would have to use reflection to get the attribute from the property.

如果使用映射文件,则可以解析映射文件。它是相当简单的xml,因此一个简单的xpath查询可以获得列名。如果使用属性,则必须使用反射从属性中获取属性。

#2


0  

Here is some code that might help.

以下是一些可能有用的代码。

    public static string[] GetDatabaseColumnNamesFromEntityProperty(Type entityType, string propertyName)
    {
        PersistentClass aNHibernateClass = NHibernateSessionManager.Instance.GetNHibernateConfiguration().GetClassMapping(entityType);

        if (aNHibernateClass == null)
        {
            return null;
        }
        else
        {
            string[] columnNames = null;

            try
            {
                Property aProperty = aNHibernateClass.GetProperty(propertyName);
                columnNames = new string[aProperty.ColumnCollection.Count];

                int count = 0;

                foreach (Column column in aProperty.ColumnCollection)
                {
                    columnNames[count] = column.Name;
                    count++;
                }
            }
            catch(Exception)
            {
                Property aProperty = aNHibernateClass.IdentifierProperty;

                //if(aProperty.Name.Equals(propertyName))
                //{
                    columnNames = new string[aProperty.ColumnCollection.Count];
                    int count = 0;

                    foreach (Column column in aProperty.ColumnCollection)
                    {
                        columnNames[count] = column.Name;
                        count++;
                    }
                //}
            }

            return columnNames;
        }
    }