3

にマップされているかどうかを見つけることはプロパティは、フィールドにマップされているかどうかを確認するにはどのような方法があります。 私は、これは、「検索のような一般的な」のようなものを生成したいと思います:私はNHibernateのにマップされていないプロパティを追加NHibernateは:プロパティは、フィールド

string[] words. 
    words = search.Split(' '); 
    Type type = typeof(T); 

    Disjunction disjunction = new Disjunction(); 
    foreach (System.Reflection.PropertyInfo property in type.GetProperties()) 
    { 
     if ((property.PropertyType == typeof(string))) 
     { 

      foreach (string word in words) 
      { 
       disjunction.Add(
        Expression.InsensitiveLike(
         property.Name, 
         "%" + word + "%")); 
      } 
     } 
    } 

場合の検索は、プロパティを解決できませんでした」の説明をNHibernate.QueryExceptionをスローします:のテキスト1:C」

私はこのような性質をマッピングしています:

class C 
{  
    [Property(0, Column = "comment")] 
    public virtual string Comment {get; set;} 
} 

答えて

4

はNHibernateのメタデータAPIを使用してください。

ISessionFactory sessionFactory; 

Type type = typeof(T); 
IClassMetadata meta = sessionFactory.GetClassMetadata(type); 

Disjunction disjunction = new Disjunction(); 
foreach (string mappedPropertyName in meta.PropertyNames) 
{ 
    IType propertyType = meta.GetPropertyType(mappedPropertyName); 

    if (propertyType == NHibernateUtil.String) 
    { 
     foreach (string word in words) 
     { 
      disjunction.Add(
       Expression.InsensitiveLike(
        mappedPropertyName, 
        "%" + word + "%")); 
     } 
    } 
} 
+0

ソリューションが働いていること、ありがとうございます! – bernhardrusch

関連する問題