刚学Nhibernate框架,就出了问题,哪位大虾指点一下,在线等,错误是:
用户代码未处理 System.Collections.Generic.KeyNotFoundException
  Message="给定关键字不在字典中。"
  Source="mscorlib"
  StackTrace:
       在 System.ThrowHelper.ThrowKeyNotFoundException()
       在 System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       在 NHibernate.Dialect.Dialect.GetDialect(IDictionary`2 props) 位置 c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Dialect\Dialect.cs:行号 173
       在 NHibernate.Cfg.SettingsFactory.BuildSettings(IDictionary`2 properties) 位置 c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\SettingsFactory.cs:行号 32
       在 NHibernate.Cfg.Configuration.BuildSettings() 位置 c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs:行号 1424
       在 NHibernate.Cfg.Configuration.BuildSessionFactory() 位置 c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs:行号 983
       在 _Default.Button1_Click(Object sender, EventArgs e) 位置 c:\Documents and Settings\Administrator\桌面\新建文件夹\TZservice\WebSite1\Default.aspx.cs:行号 30
       在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
 NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
        cfg.AddAssembly("TZService.Model");
        ISessionFactory factory = cfg.BuildSessionFactory();
        ISession session = factory.OpenSession();
        ITransaction transaction = session.BeginTransaction();
        session = factory.OpenSession();
        UserInfo item = new UserInfo();
        item.UseName = "zhouwu";
        item.Md5Password = "123456";
        item.UpperUseName = "ZHOUWU";
        session.Save(item);        // commit all of the changes to the DB and close the ISession
        transaction.Commit();
        session.Close();

解决方案 »

  1.   

    我也遇到了和楼主同样的问题,楼主要是解决了就麻烦给我发封邮件,谢谢,[email protected]
      

  2.   

    你代码问题真多啊。
    cfg 对象都写错了。
    应该是加上cfg.Configure(); NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); 
     cfg.Configure();
     cfg.AddAssembly("TZService.Model"); 
      

  3.   

    小孩子也刚遇到这样的问题,刚在网上搜了一下,便搜到楼主的博客了!不知道怎么解决,如果楼主解决了,麻烦告诉小孩子一下,谢谢[email protected]
      

  4.   

    我也是刚学Nhibernate,也是弄了这个例子,遇到和你同样的问题
    我的解决方式:
    例子中web.config的配置方式已经过时了,用这种方式Nhibernate找不到配置文件
    我采用下面的配置方式:
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
          <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
          <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
          <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
          <property name="connection.connection_string">server=LIXL2073A\SQLEXPRESS;uid=sa;pwd=salxl;database=TestNHibernate</property>
          <property name="show_sql">true</property>
        </session-factory>
      </hibernate-configuration>再一个需要修改一下SessionFactory中的BuildSessionFactory这个方法private static void BuildSessionFactory(string AssemblyName)
            {
                cfg = new Configuration();
                //这里是后添加的
                cfg.Configure();
                //cfg.AddAssembly(System.Reflection.Assembly.Load("Web.Model"));
                cfg.AddAssembly(AssemblyName);
                sessions = cfg.BuildSessionFactory();        }
    我就改了这两个地方,希望对你有帮助,呵呵。
            }