初探SchemaUpdate工具

   以前开发的时候都是根据文档中的内容来建立数据库然后再启动项目。开发之前建立数据库在开发过程中会遇到一些不必要的麻烦,使得我们不得不以数据库为中心开发,数据库设计好了开发起来容易些,如果数据库设计不好那么给我们的开发会带来多大的麻烦啊!为了解决这个问题在我采用领域驱动设计。

  领域驱动设计就是先编写持久化类和映射文件,然后使用SchemaExport工具生成数据库架构。我个人的理解是先建立对象模型,然后根据对象模型来生成数据库。这也是面向对象开发的步骤和我们传统的开发有点背。

  NHibernate的hbm2dll提供SchemaExport工具,这个工具可以根据对象模型自动生成数据库,废话就少说了看下面的实例吧,代码是最有说服力的。

首先写持久化类

 

代码
 
   
1
2   namespace DDDTest
3
4
5 [DisplayName( " 员工 " )]
6 [Class(Table = " Employees " , NameType = typeof (Employee), Schema = " dbo " )]
7 public class Employee : DomainModel
8 {
9 public Employee()
10 {
11
12 }
13
14 [DisplayName( " 编号 " )]
15 [Id(Name = " ID " , Column = " EmployeeID " )]
16 [Generator( 1 , Class = " native " )]
17 virtual public int ID { get ; set ; }
18
19 [DisplayName( " 姓名 " )]
20 [Property(Length = 200 , NotNull = true , Unique = true )]
21 virtual public string Name { get ; set ; }
22
23 [DisplayName( " 生日 " )]
24 [Property(NotNull = false )]
25 virtual public DateTime ? Birthday { get ; set ; }
26
27 }
28 }
29  

然后写XML文件按照新映射架构数据库(真正开发的时候XML可以不用写可以用hibernate.cfg.xml我这里只是一个演示)

 

代码
 
   
<? xml version = " 1.0 " encoding = " utf-8 " ?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it
in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
< hibernate - configuration xmlns = " urn:nhibernate-configuration-2.2 " >
< session - factory name = " NHibernate.Test " >
< property name = " connection.driver_class " > NHibernate.Driver.SqlClientDriver </ property >
< property name = " connection.connection_string " >
Server
= (local);initial catalog = FF;Integrated Security = SSPI
</ property >
< property name = " adonet.batch_size " > 10 </ property >
< property name = " show_sql " > false </ property >
< property name = " dialect " > NHibernate.Dialect.MsSql2005Dialect </ property >
< property name = " use_outer_join " > true </ property >
< property name = " command_timeout " > 60 </ property >
< property name = " query.substitutions " > true 1 , false 0 , yes ' Y ' , no ' N ' </ property >
< property name = " proxyfactory.factory_class " > NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </ property >
</ session - factory >
</ hibernate - configuration >

最后一步就是用SchemaUpdate工具生成数据库了

 

代码
 
   
1 public static void CreadDB()
2 {
3 var connectionString = Configuration.GetProperty( " connection.connection_string " );
4
5 SqlServer05Helper.ReCreateDatabase(connectionString);
6 SqlServer05Helper.CreateSchemasOnDbo(connectionString, Configuration.GetAllSchemas());
7 SchemaExport export = new SchemaExport(Configuration);
8 export.Create( true , true );
9
10 }

 SqlServer05Helper.ReCreateDatabase(connectionString);
  SqlServer05Helper.CreateSchemasOnDbo(connectionString, configuration.GetAllSchemas());

这俩句用了一个扩展类SqlServer05Helper.CS主用是生成一个数据库的架构

 

var schemaExport = new SchemaExport(configuration);
            schemaExport.Create(true, true)

这俩句主要就是生成表的。

 

在这里有主要写了下SchemaExport工具怎样生成数据库,SchemaExport还可以更新删除数据库。

转载于:https://www.cnblogs.com/changminglong/archive/2010/09/28/1837838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值