NHibernate Fluent并命名为Queries

时间:2009-11-26 15:15:43

标签: .net nhibernate

我有以下名为Queryé的结果,我希望将其映射到类上。

<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时使用了无效或不完整的配置。检查PotentialReasons集合,以及InnerException以获取更多详细信息。

InnerExceptioné“字典中没有给定的密钥。”

如果我在没有返回类的情况下使用Query,那么除了我得到的返回值没有映射到类之外它运行良好。但是Class绝对是在集会中。 如何将查询映射到类?

非常感谢

2 个答案:

答案 0 :(得分:0)

经过一些黑客攻击后,我确信我只能将某些SP的输出映射到一个具有数据库映射的类。

所以只需要一个sp并将其映射到某个类就行不通了。该类必须具有Mapping,因此该类必须是域模型的类。

答案 1 :(得分:0)

我没有任何类映射到数据库。下面的代码对我有用。 Desk在Desk.hbm.xml文件中定义的位置。

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();
    }
}