c#,NHibernate,ASP.NET2.0,Winform
2008-06-18 00:26 Virus-BeautyCode 阅读(2192) 评论(2) 编辑 收藏 举报using System;
using System.Collections;
namespace Model
{
Person
}
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.Person, Model" table="Person" lazy="false">
<id name="Id" type="Int32" unsaved-value="null">
<column name="id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Person"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="name" length="50" sql-type="varchar" not-null="false"/>
</property>
</class>
</hibernate-mapping>
以上两个文件都是用codesmith自动生成的,很好用,里面有NHibernate模板。<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.Person, Model" table="Person" lazy="false">
<id name="Id" type="Int32" unsaved-value="null">
<column name="id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Person"/>
<generator class="native" />
</id>
<property name="Name" type="String">
<column name="name" length="50" sql-type="varchar" not-null="false"/>
</property>
</class>
</hibernate-mapping>
有三个注意事项:
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.
<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.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.connection.connection_string" value="server=127.0.0.1;uid=virus;pwd=123.com;database=TestDB" />
<add key="hibernate.connection.isolation" value="ReadCommitted"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
</nhibernate>
下面是c#的代码
<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.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.connection.connection_string" value="server=127.0.0.1;uid=virus;pwd=123.com;database=TestDB" />
<add key="hibernate.connection.isolation" value="ReadCommitted"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
</nhibernate>
下面是c#的代码
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration().AddAssembly("Model");
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();
Person person = new Person();
person.Name = "swb";
ITransaction tran = session.BeginTransaction();
try
{
session.Save(person);
tran.Commit();
Response.Write("this is ok.");
}
catch (Exception ex)
{
tran.Rollback();
Response.Write(ex.Message);
}
finally
{
session.Close();
}
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();
Person person = new Person();
person.Name = "swb";
ITransaction tran = session.BeginTransaction();
try
{
session.Save(person);
tran.Commit();
Response.Write("this is ok.");
}
catch (Exception ex)
{
tran.Rollback();
Response.Write(ex.Message);
}
finally
{
session.Close();
}