c#,NHibernate,ASP.NET2.0,Winform
c#,NHibernate,ASP.NET2.0,Winform
using System;
c#,NHibernate,ASP.NET2.0,Winform
using System.Collections;
c#,NHibernate,ASP.NET2.0,Winform
c#,NHibernate,ASP.NET2.0,Winform
c#,NHibernate,ASP.NET2.0,Winform
namespace Model
c#,NHibernate,ASP.NET2.0,Winform<?xml version="1.0" encoding="utf-8" ?>
c#,NHibernate,ASP.NET2.0,Winform
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
c#,NHibernate,ASP.NET2.0,Winform    
<class name="Model.Person, Model" table="Person" lazy="false">
c#,NHibernate,ASP.NET2.0,Winform        
<id name="Id" type="Int32" unsaved-value="null">
c#,NHibernate,ASP.NET2.0,Winform            
<column name="id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Person"/>
c#,NHibernate,ASP.NET2.0,Winform            
<generator class="native" />
c#,NHibernate,ASP.NET2.0,Winform        
</id>
c#,NHibernate,ASP.NET2.0,Winform        
<property name="Name" type="String">
c#,NHibernate,ASP.NET2.0,Winform            
<column name="name" length="50" sql-type="varchar" not-null="false"/>
c#,NHibernate,ASP.NET2.0,Winform        
</property>
c#,NHibernate,ASP.NET2.0,Winform    
</class>
c#,NHibernate,ASP.NET2.0,Winform
</hibernate-mapping>
c#,NHibernate,ASP.NET2.0,Winform
以上两个文件都是用codesmith自动生成的,很好用,里面有NHibernate模板。

有三个注意事项:
1、提示什么"Unknown entity class"之类的
请注意映射文件的<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">,一定要是2.2
2、
 如果提示实体类的属性需要加上virtual关键字,就需要在<class name="Model.Person, Model" table="Person" lazy="false">中加上 lazy="false"
3、
提示什么"Unknown entity class"之类的
右键实体类对应的Person.hbm.xml需要作为嵌入资源,右键文件,属性,Build Action设置为嵌入资源

下面是web.config文件中的内容,指明数据库的位置和类型,如果提示数据库没有打开远程访问之类的信息,需要在sqlserver 外围应用配置器中中打开本地连接和远程连接,同时启用命名管道和TCP/IP,在sqlserver configuration manager的sql2005协议将tcp/ip双击打开,将IP地址tab中的IPALL的TCP动态端口改为默认的1433.
c#,NHibernate,ASP.NET2.0,Winform <configSections>
c#,NHibernate,ASP.NET2.0,Winform      
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,
c#,NHibernate,ASP.NET2.0,Winform                    Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
 />
c#,NHibernate,ASP.NET2.0,Winform    
</configSections>
c#,NHibernate,ASP.NET2.0,Winform
c#,NHibernate,ASP.NET2.0,Winform    
<nhibernate>
c#,NHibernate,ASP.NET2.0,Winform      
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
c#,NHibernate,ASP.NET2.0,Winform      
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
c#,NHibernate,ASP.NET2.0,Winform      
<add key="hibernate.connection.connection_string" value="server=127.0.0.1;uid=virus;pwd=123.com;database=TestDB" />
c#,NHibernate,ASP.NET2.0,Winform      
<add key="hibernate.connection.isolation" value="ReadCommitted"/>
c#,NHibernate,ASP.NET2.0,Winform      
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
c#,NHibernate,ASP.NET2.0,Winform    
</nhibernate>

下面是c#的代码

c#,NHibernate,ASP.NET2.0,WinformNHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration().AddAssembly("Model");
c#,NHibernate,ASP.NET2.0,Winform        ISessionFactory factory 
= config.BuildSessionFactory();
c#,NHibernate,ASP.NET2.0,Winform        ISession session 
= factory.OpenSession();
c#,NHibernate,ASP.NET2.0,Winform
c#,NHibernate,ASP.NET2.0,Winform        Person person 
= new Person();
c#,NHibernate,ASP.NET2.0,Winform        person.Name 
= "swb";
c#,NHibernate,ASP.NET2.0,Winform
c#,NHibernate,ASP.NET2.0,Winform        ITransaction tran 
= session.BeginTransaction();
c#,NHibernate,ASP.NET2.0,Winform        
try
        }

相关文章:

  • 2022-12-23
  • 2021-08-31
  • 2021-06-15
  • 2021-04-25
  • 2021-12-25
  • 2021-07-21
猜你喜欢
  • 2022-12-23
  • 2021-03-31
  • 2021-10-19
  • 2022-12-23
  • 2021-12-25
相关资源
相似解决方案