2009-11-26 86 views
0

我有以下名爲查特,其結果我喜歡映射到一個類。NHibernate流利和命名查詢

<sql-query name="GetIndustryCodes"> 
    <return class="IndustryCode"> 
     <return-property name="Sector_kategorie" column="Sector_kategorie"/> 
     <return-property name="Sector_scheme_reference" column="Sector_scheme_reference"/> 
     <return-property name="Group_Beschreibung" column="Group_Beschreibung"/>  
    </return>   
    <![CDATA[ 
     select * from 
    (
     select  
    lvl1.sprach_iso_cd, 
     lvl1.kategorie Sector_kategorie, 
     lvl1.scheme_reference Sector_scheme_reference, 
     lvl1.Beschreibung Sector_Beschreibung, 

     lvl2.kategorie Group_kategorie, 
     lvl2.scheme_reference Group_scheme_reference, 
     lvl2.Beschreibung Group_Beschreibung, 

     lvl3.kategorie Industry_kategorie, 
     lvl3.scheme_reference Industry_scheme_reference, 
     lvl3.Beschreibung Industry_Beschreibung, 

     lvl4.kategorie Subindustry_kategorie , 
     lvl4.scheme_reference Subindustry_scheme_reference, 
     lvl4.Beschreibung Subindustry_Beschreibung 

     from StagingDb.Rds.GR3_S_R10916_TX lvl1 

     left join StagingDb.Rds.GR3_S_R10916_TX lvl2 
     on lvl2.sprach_iso_cd = lvl1.sprach_iso_cd 
     and lvl2.kategorie = 'MSCIS&PGroup' 
     and lvl1.scheme_reference = substring(lvl2.scheme_reference,1,2) 

     left join StagingDb.Rds.GR3_S_R10916_TX lvl3 
     on lvl3.sprach_iso_cd = lvl1.sprach_iso_cd 
     and lvl3.kategorie = 'MSCIS&PIndustry' 
     and lvl2.scheme_reference = substring(lvl3.scheme_reference,1,4) 

     left join StagingDb.Rds.GR3_S_R10916_TX lvl4 
     on lvl4.sprach_iso_cd = lvl1.sprach_iso_cd 
     and lvl4.kategorie = 'MSCIS&P' 
     and lvl3.scheme_reference = substring(lvl4.scheme_reference,1,6) 

     where lvl1.kategorie = 'MSCIS&PSector' 

) t 
    where t.sprach_iso_cd ='en' 
    ]]> 
    </sql-query> 

另外我有一個C#類

public class IndustryCode 
    { 
     public virtual string Sector_kategorie { get; set; } 
     public virtual string Sector_scheme_reference { get; set; } 
     public virtual string Sector_Beschreibung { get; set; } 
     public virtual string Group_kategorie { get; set; } 
     public virtual string Group_scheme_reference { get; set; } 
     public virtual string Group_Beschreibung { get; set; } 
     public virtual string Industry_kategorie { get; set; } 
     public virtual string Industry_scheme_reference { get; set; } 
     public virtual string Industry_Beschreibung { get; set; } 
     public virtual string Subindustry_kategorie { get; set; } 
     public virtual string Subindustry_scheme_reference { get; set; } 
     public virtual string Subindustry_Beschreibung { get; set; }   

    } 

現在同時啓動程序,我得到在創建一個SessionFactory的exceptioné 一個無效或不完整的配置使用。請參閱PotentialReasons集合和InnerException以獲取更多詳細信息。

InnerExceptioné「給定的鍵不在字典中。」

如果我使用的查詢,而無需返回類,那麼它工作得很好,只是我沒有得到映射到一個類的返回值。但是班級肯定在大會上。 如何將查詢映射到類中?

非常感謝你

回答

0

一些黑客我相信,我可以將某些SP的輸出只映射到具有映射到數據庫的下課。

所以只要有一個sp並將其映射到某個類將不起作用。該類必須具有映射,因此該類必須是域模型的一個類。

0

我沒有任何類映射到數據庫。下面的代碼爲我工作。在Desk.hbm.xml文件中定義Desk的位置。

public class NHibernateHelper 
{ 
    private readonly string _connectionString; 
    private ISessionFactory _sessionFactory; 

    public NHibernateHelper(string connectionString) 
    { 
     _connectionString = connectionString; 
    } 

    public ISessionFactory SessionFactory 
    { 
     get { return _sessionFactory ?? (_sessionFactory = CreateSessionFactory()); } 
    } 

    private ISessionFactory CreateSessionFactory() 
    { 
     return Fluently.Configure() 
       .Database(MsSqlConfiguration.MsSql2008.ConnectionString(_connectionString)) 
       .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateHelper>()) 
       .Mappings(m => m.HbmMappings.AddClasses(typeof(Desk))) 
       .BuildSessionFactory(); 
    } 
}