首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

问个Nhibernate的入门知识啊小弟我写的一个简单实例总调不出来

2012-01-31 
问个Nhibernate的入门知识啊,我写的一个简单实例总调不出来我在那个官网下了最新的版本。建立一个window应

问个Nhibernate的入门知识啊,我写的一个简单实例总调不出来
我在那个官网下了最新的版本。
建立一个window应用程序。在“引用”里面添加了Nhibernate.dll
这是类文件:
using   System;
using   System.Collections.Generic;
using   System.Text;

namespace   Nhibernate
{
        class   User
        {

                private   string   id;
                private   string   userName;
                private   string   password;
                private   string   emailAddress;
                private   DateTime   lastLogon;

                public   string   Id
                {
                        get   {   return   id;   }
                        set   {   id   =   value;   }
                }

                public   string   UserName
                {
                        get   {   return   userName;   }
                        set   {   userName   =   value;   }
                }

                public   string   Password
                {
                        get   {   return   password;   }
                        set   {   password   =   value;   }
                }

                public   string   EmailAddress
                {
                        get   {   return   emailAddress;   }
                        set   {   emailAddress   =   value;   }
                }

                public   DateTime   LastLogon
                {
                        get   {   return   lastLogon;   }
                        set   {   lastLogon   =   value;   }
                }

        }
}
这是映射文件:
<?xml   version= "1.0 "   encoding= "utf-8 "   ?>
<hibernate-mapping   xmlns= "urn:nhibernate-mapping-2.0 ">


    <class   name= "NHibernate.User,   NHibernate "   table= "users ">
        <id   name= "Id "   column= "LogonId "   type= "String "   length= "20 ">
            <generator   class= "assigned "   />
        </id>
        <property   name= "UserName "   column= "Name "   type= "String "   length= "40 "/>
        <property   name= "Password "   type= "String "   length= "20 "/>
        <property   name= "EmailAddress "   type= "String "   length= "40 "/>
        <property   name= "LastLogon "   type= "DateTime "/>
    </class>
</hibernate-mapping>
这是配置文件:
<?xml   version= "1.0 "   encoding= "utf-8 "   ?>
<configuration>
    <configSections>
        <section   name= "nhibernate "   type= "System.Configuration.NameValueSectionHandler,   System,   Version=1.0.3300.0,Culture=neutral,   PublicKeyToken=b77a5c561934e089 "   />
    </configSections>
    <nhibernate>

        <add   key= "hibernate.connection.provider "   value= "NHibernate.Connection.DriverConnectionProvider "   />

        <add   key= "hibernate.dialect "   value= "NHibernate.Dialect.MsSql2000Dialect "   />

        <add   key= "hibernate.connection.driver_class "   value= "NHibernate.Driver.SqlClientDriver "   />

        <add   key= "hibernate.connection.connection_string "   value= "Server=localhost;initial   catalog=Nhibernate;User   ID=sa;Password=;Min   Pool   Size=2 "   />

    </nhibernate>

</configuration>
这是程序运行的代码段:
  Configuration   cfg   =   new   Configuration();
                        cfg.AddAssembly( "NHibernate ");
                        ISessionFactory   factory   =   cfg.BuildSessionFactory();
                        ISession   session   =   factory.OpenSession();
                        ITransaction   transaction   =   session.BeginTransaction();
                        User   newUser   =   new   User();
                        newUser.Id   =   "joe_cool ";
                        newUser.UserName   =   "Joseph   Cool ";
                        newUser.Password   =   "abc123 ";


                        newUser.EmailAddress   =   "joe@cool.com ";
                        newUser.LastLogon   =   DateTime.Now;

                        //   Tell   NHibernate   that   this   object   should   be   saved
                        try
                        {
                                session.Save(newUser);
                        }
                        catch   (Exception   e1)
                        {
                                MessageBox.Show(e1.ToString());
                        }

                        //   commit   all   of   the   changes   to   the   DB   and   close   the   ISession
                        transaction.Commit();
                        session.Close();
就是Save的时候报错:
未处理   NHibernate.MappingException
    Message= "Unknown   entity   class:   Nhibernate.User "
    Source= "NHibernate "
    StackTrace:
              在   NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type   theClass)
              在   NHibernate.Impl.SessionImpl.GetClassPersister(Type   theClass)
              在   NHibernate.Impl.SessionImpl.GetEntityPersister(Object   obj)
              在   NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object   obj,   CascadingAction   action,   Object   anything)
              在   NHibernate.Impl.SessionImpl.Save(Object   obj)
              在   Nhibernate.Form1.button1_Click(Object   sender,   EventArgs   e)   位置   E:\WinPro\Nhibernate\Nhibernate\Form1.cs:行号   41
              在   System.Windows.Forms.Control.OnClick(EventArgs   e)
              在   System.Windows.Forms.Button.OnClick(EventArgs   e)
              在   System.Windows.Forms.Button.OnMouseUp(MouseEventArgs   mevent)
              在   System.Windows.Forms.Control.WmMouseUp(Message&   m,   MouseButtons   button,   Int32   clicks)
              在   System.Windows.Forms.Control.WndProc(Message&   m)
              在   System.Windows.Forms.ButtonBase.WndProc(Message&   m)


              在   System.Windows.Forms.Button.WndProc(Message&   m)
              在   System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&   m)
              在   System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&   m)
              在   System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr   hWnd,   Int32   msg,   IntPtr   wparam,   IntPtr   lparam)
              在   System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&   msg)
              在   System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32   dwComponentID,   Int32   reason,   Int32   pvLoopData)
              在   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32   reason,   ApplicationContext   context)
              在   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32   reason,   ApplicationContext   context)
              在   System.Windows.Forms.Application.Run(Form   mainForm)
              在   Nhibernate.Program.Main()   位置   E:\WinPro\Nhibernate\Nhibernate\Program.cs:行号   17
              在   System.AppDomain.nExecuteAssembly(Assembly   assembly,   String[]   args)
              在   System.AppDomain.ExecuteAssembly(String   assemblyFile,   Evidence   assemblySecurity,   String[]   args)
              在   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
              在   System.Threading.ThreadHelper.ThreadStart_Context(Object   state)
              在   System.Threading.ExecutionContext.Run(ExecutionContext   executionContext,   ContextCallback   callback,   Object   state)
              在   System.Threading.ThreadHelper.ThreadStart()


[解决办法]
映射出错。要保证你的映射文件和class文件在一个目录.另外,假设class文件是user.cs,那么映射文件应该是user.hbm.xml.另外,property最好使用virtual的。
参考官方的文档:
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/quickstart.html
[解决办法]
Nhibernate.User 与NHibernate.User
似乎并不相同
[解决办法]
<class name= "NHibernate.User
namespace Nhibernate
{
class User
{
[解决办法]
cfg.AddAssembly( "NHibernate ");

最好 不要用NHibernate的命名空间,随便取一个,TestNHibernate也好啊
[解决办法]
老大 "NHibernate.User,这个是类名,NHibernate(命名空间,这个命名空间下有User类吗?).User(类名)
你全类的名是Nhibernate.User而不是NHibernate.User
[解决办法]
C#应该是大小写敏感的,如果是我记错了,那就不好意思了,你自己查找问题吧
------解决方案--------------------


cfg.AddAssembly( "NHibernate ");
这一句是否也改了呢?
[解决办法]
Configuration cfg = new Configuration().Configure();
[解决办法]
你可以不要看我上面说的
还需引用Iesi.CollectionsDLL
nhibernate-mapping.xsd这个文件和映射文件放同一目录

<class name= "映射类所在命名空间.映射类名, 映射类所在的程序集名 " table= "映射的数据库表 ">

最近也在看这个东西,写了一个DEMO,想交流的话,留下你的EMAIL


[解决办法]
已发,请注意查收

热点排行