用Codesmith 5.2的Nhibernate模板从ORACLE数据库生成实体类和数据访问类
其实也是看了codesmith的video(http://www.codesmithtools.com/video/nhibernate.html)做的,虽然作者基于的前一个版本(vs2008,nhibernate2.0),但和最新的3.0 nhibernate模板操作是一样的
开始之前
你的电脑要有vs2010
安装codesmith,我的是5.2版本的
下载最新的nhibernate模板:http://code.google.com/p/codesmith/downloads/detail?name=NHibernate-v1.2.1.2125.zip&can=2&q=
有一个oracle数据库可以连接
有着了这些,就可以开始了
1. 新建一个class library 的工程
2. 在工程中加一个csp工程文件
内容如下:
<?xml version="1.0"?>
<codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd">
<variables>
<add key="ConnectionString1" value="Data Source=localhost;Initial Catalog=Petshop;Integrated Security=True" />
<add key="ConnectionString2" value="data source=localhost;persist security info=True;user id=hr;password=hr" />
</variables>
<propertySets>
<propertySet name="NHibernateMaster" template="..\..\Templates\CSharp\NHibernateMaster.cst">
<property name="VersionColumn">^((R|r)ow)?(V|v)ersion$</property>
<property name="NHibernateVersion">v3_0</property>
<property name="VsVersion">VS_2010</property>
<property name="AssemblyName">Sample.Data</property>
<property name="ManagerNamespace">Sample.Data.ManagerObjects</property>
<property name="BusinessNamespace">Sample.Data.BusinessObjects</property>
<property name="BaseNamespace">Sample.Data.Base</property>
<property name="UnitTestNamespace">Sample.Data.UnitTests</property>
<property name="SourceDatabase">
<connectionString>$(ConnectionString2)</connectionString>
<providerType>SchemaExplorer.OracleSchemaProvider,SchemaExplorer.OracleSchemaProvider</providerType>
</property>
</propertySet>
</propertySets>
</codeSmith>
<codeSmith xmlns="http://www.codesmithtools.com/schema/csp.xsd">
<variables>
<add key="ConnectionString1" value="Data Source=localhost;Initial Catalog=Petshop;Integrated Security=True" />
<add key="ConnectionString2" value="data source=localhost;persist security info=True;user id=hr;password=hr" />
</variables>
<propertySets>
<propertySet name="NHibernateMaster" template="..\..\Templates\CSharp\NHibernateMaster.cst">
<property name="VersionColumn">^((R|r)ow)?(V|v)ersion$</property>
<property name="NHibernateVersion">v3_0</property>
<property name="VsVersion">VS_2010</property>
<property name="AssemblyName">Sample.Data</property>
<property name="ManagerNamespace">Sample.Data.ManagerObjects</property>
<property name="BusinessNamespace">Sample.Data.BusinessObjects</property>
<property name="BaseNamespace">Sample.Data.Base</property>
<property name="UnitTestNamespace">Sample.Data.UnitTests</property>
<property name="SourceDatabase">
<connectionString>$(ConnectionString2)</connectionString>
<providerType>SchemaExplorer.OracleSchemaProvider,SchemaExplorer.OracleSchemaProvider</providerType>
</property>
</propertySet>
</propertySets>
</codeSmith>
3. 更改csp文件中template的的位置,指向新下载的template,并配置oracle数据的链接
4. 然后就可以生成实体文件了
5. 实体生成好了以后,创建一个console applicaiton去测试一下
5. 在console applicaiton中加一个app.config,内容如下,注意你的数据库的版本,这里用的10g的数据库
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=xe)));User Id=hr;Password=hr;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Sample.Data"/>
</session-factory>
</hibernate-configuration>
</configuration>
6. 写一个main函数,就搞定了
static void Main(string[] args)
{
try
{
IManagerFactory managerFactory = new ManagerFactory();
using (IJobManager categoryManager = managerFactory.GetJobManager())
{
Job job = new Job();
var lst = categoryManager.GetAll();
System.Console.WriteLine(lst.Count + "");
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
System.Console.ReadLine();
}
}