其实网络里有了很多相关的资料和资源.因为第一次接触nbernate的人不少,可惜会遇到很多不同的问题.特意把我自己在使用的过程中遇到的问题给大家讲解一下.希望对新手有少少作用

网络里的例子 nhibernate1.2版本 +sqlserver2005+ vs2003

1.建立一个项目,名字叫ClassLibrary

 2.新建立一个类 user

 ClassLibrary
{
    public class User
    {
        
private string id;
        
private string userName;
        
private string password;
        
private string emailAddress;
        
private DateTime lastLogon;


        
public User()
        {
        }

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

    }
}

 

3. 新建立一个xml文件   User.hbm.xml

这里要注意这一段<class name="ClassLibrary.User, ClassLibrary" table="users" lazy="false">

有一点必须注意的是,必须把该xml文件右键属性设置为生成操作-->嵌入的资源

 ClassLibrary是你的程序集名称和默认空间


    <class name="ClassLibrary.User, ClassLibrary" table="users" lazy="false">
        
<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>

 

 

4.编译好dll文件

5.新增加一个项目Nhibernate1 (使用的是sqlserver2005数据库)

 添加一个app.config文件

 

 

 


    <configSections>
        
<section
          
name="nhibernate" 
          type
="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
    
/>
    
</configSections>
    
<nhibernate>
        
<add key="hibernate.connection.provider"
          value
="NHibernate.Connection.DriverConnectionProvider"
    
/>
        
<add key="hibernate.dialect"
          value
="NHibernate.Dialect.MsSql2005Dialect"
    
/>
        
<add key="hibernate.connection.driver_class"
          value
="NHibernate.Driver.SqlClientDriver"
    
/>
        
<add key="hibernate.connection.connection_string"
          value
="Server=a059\PMSERVER;initial catalog=test;User ID=sa;Password=;"
    
/>

    
</nhibernate>
</configuration>

 

6.在test库里建立一个数据表 User

 

7.建立一个窗体.然后把下面代码贴入

 

 

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using NHibernate.Cfg;

Using ClassLibrary;

namespace Nhibernate1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Configuration cfg = new Configuration();
            cfg.AddAssembly("ClassLibrary");

            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            User newUser = new User();
            newUser.Id = "joe_cool_test";
            newUser.UserName = "Joseph Cool";
            newUser.Password = "abc123";
            newUser.EmailAddress = "joe@cool.com";
            newUser.LastLogon = DateTime.Now;

            // Tell NHibernate that this object should be saved
            session.Save(newUser);

            // commit all of the changes to the DB and close the ISession
            transaction.Commit();
            session.Close();

            MessageBox.Show("OK!");
        }
    }
}

 遇到的错误

1.

 NHibernate.MappingException:   Unknown   entity   class:

把xml文件嵌入的资源一般可以解决

2.

cfg.AddAssembly("Nhibernate12");

 未处理MappingException

Could not add assembly Nhiberate

把库类设置好一般都可以解决该问题

3.Winform其实不必要分开两个项目来实现,可以使用一个项目来实现的,原理一致

 4.因为ASP.NET没有办法设置namespace,WebForm所以引用两个项目就有必要,按照上面的代码 ASP.Net测试也成功

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2021-09-05
  • 2021-09-07
猜你喜欢
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2021-09-15
  • 2021-05-25
相关资源
相似解决方案