刚学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();
用户代码未处理 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();
解决方案 »
- 如何读取 XML指定节点的值?
- 怎么调用propertygrid属性页中的值?
- 急啊!请问一下怎么在picturebox里面画图前刷新picturebox?
- Winform如何递归遍历TreeView,哪位给个代码,谢谢
- 谁有C#中listview关于数据库添加,删除,修改的例子给小弟发一个,不胜感激。[email protected]
- 选中 DataGridView 控件的行
- c#调试时采用反汇编方式,如何像vc6.0那样跟进call?
- 我的方案如何?高的快!
- 能通过ODBC.net读取DBF,但是无法读取DBF中逻辑删除的行
- 能帮我看一下么?谢谢!!
- C#中,最深奥的是什么?
- 这种错误怎么解决
cfg 对象都写错了。
应该是加上cfg.Configure(); NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
cfg.AddAssembly("TZService.Model");
我的解决方式:
例子中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(); }
我就改了这两个地方,希望对你有帮助,呵呵。
}