给定关键字不在字典中,ISessionFactory factory = cfg.BuildSessionFactory();

zhouwu0716 2008-04-12 02:14:37
刚学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();
...全文
1188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
在路上2021 2008-11-13
  • 打赏
  • 举报
回复
我也是刚学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();

}
我就改了这两个地方,希望对你有帮助,呵呵。
}
竹木一540 2008-06-01
  • 打赏
  • 举报
回复
小孩子也刚遇到这样的问题,刚在网上搜了一下,便搜到楼主的博客了!不知道怎么解决,如果楼主解决了,麻烦告诉小孩子一下,谢谢zhipingok@yahoo.com.cn
Bingsun 2008-04-19
  • 打赏
  • 举报
回复
你代码问题真多啊。
cfg 对象都写错了。
应该是加上cfg.Configure();

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
cfg.AddAssembly("TZService.Model");
biser 2008-04-19
  • 打赏
  • 举报
回复
我也遇到了和楼主同样的问题,楼主要是解决了就麻烦给我发封邮件,谢谢,biser007@gmail.com

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧