0
我想知道是否有人知道如何使用NHibernate找到实体属性映射到的列,并且只有IEntityPersister
接口可用。查找实体属性映射到的数据库列
我想知道是否有人知道如何使用NHibernate找到实体属性映射到的列,并且只有IEntityPersister
接口可用。查找实体属性映射到的数据库列
如果您使用映射文件,则可以解析该映射文件。这是相当简单的XML,所以一个简单的xpath查询会得到你的列名。如果您使用属性,则必须使用反射才能从属性中获取属性。
以下是一些可能有所帮助的代码。
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;
}
}
我曾考虑过这样做。我只是希望在IEntityPersister接口中有一个over/miss命名的属性,它让我可以在不需要解析的情况下找到列名。 – MisterHux 2009-08-20 14:58:47