C# 查找实体的数据库列';的属性映射到
我想知道是否有人知道如何使用NHibernate查找实体的属性映射到哪个列,并且只有C# 查找实体的数据库列';的属性映射到,c#,.net,nhibernate,nhibernate-mapping,C#,.net,Nhibernate,Nhibernate Mapping,我想知道是否有人知道如何使用NHibernate查找实体的属性映射到哪个列,并且只有IEntityPersister接口可用。如果使用映射文件,可以解析映射文件。它是相当简单的xml,因此一个简单的xpath查询将获得列名。如果要使用属性,则必须使用反射从属性中获取属性。以下是一些代码,可能会有所帮助 public static string[] GetDatabaseColumnNamesFromEntityProperty(Type entityType, string proper
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-looked/miss-named属性,它让我无需进行解析就可以找到列名。