国产化数据库迁移工具不会用?教你手搓一个万能数据迁移工具。
<h1>为什么要手搓一个自己的数据库迁移工具</h1><p>为什么要进行数据库迁移?主要有这么几种情况:</p>
<p>(1)、开发测试阶段使用的数据库类型于生产环境的数据库类型不同,如开发测试用MySQL数据库,生产环境用Oracle\SQLServer等企业级数据库,需要将开发测试环境的一些基础数据迁移到生产环境;为什么开发测试用的数据库类型与生产环境不同,自然是为了降低开发成本,生产环境的数据库是客户购买的,软件开发方未必能有条件购买使用同类型的数据库产品。</p>
<p>(2)、开发测试环境使用的数据库版本与生产环境的数据库不同,例如开发测试环境使用的是数据库的“开发版”、社区版,或者较低的版本,生产环境用的是企业版、最新的版本;这样做自然也是为了降低开发成本,比如开发环境用金仓V8版本,而线上用的是最新的金仓V9版本。</p>
<p>(3)、国产化要求,需要将之前运行在MySQL、Oracle、SQLServer等数据库上的数据迁移到金仓、达梦等国产数据库上;国产化要求是现在很多政府类项目的硬性要求,软件公司之前成熟的产品用的是非国产化数据库,现在都有数据迁移需求。</p>
<p>(4)、提高数据库性能和数据备份的要求;线上一些系统运行时间长,产生的很多历史数据需要迁移到其它数据库上以便给线上的数据库“瘦身”,起到一个数据备份和提高线上系统查询效率的作用;比如一些日志数据、报警数据,可以把归档后的数据迁移到新的数据库上。</p>
<p>在国产数据库领域,金仓数据库算是比较常用的一种数据库了,号称百分百兼容Oracle的,也基本百分百兼容MySQL等数据库,能“无缝”迁移。金仓等国产数据库在兼容性这方面的确很努力,如果这事不努力国人为什么要用你的数据库?在实际使用过程中的确感觉不到兼容性问题,但在做数据迁移的时候才发现在兼容性方面还是有一些问题,而金仓提供的数据迁移工具对于非资深用户而言还是有较高的门槛,每次遇到问题都不得不求助于金仓的技术支持人员,每次寻求他们支持都要沟通很久,于是我决定自己手搓一个数据库迁移工具,而且还能反向迁移,即从金仓迁移到MySQL等各种数据库,而不是金仓自己的工具只能从Oracle,SQL Server,MySQL,PostgreSQL这几种数据库迁移到金仓。</p>
<p>关于数据迁移,以前工作中经常进行,因此积累了一定的经验,而且还把这些经验写到《SOD框架“企业级”应用数据架构实战》这本书里面了。下面介绍一下怎么使用SOD框架手搓一个数据迁移工具,开始之前必须先了解数据迁移有哪些问题,才能明白手搓一个自己的迁移工具的好处。</p>
<h1>数据迁移的常见问题</h1>
<p>数据迁移并不是一个简单的工作,虽然很多数据库都提供了将别的数据库迁移过来的数据迁移工具,但使用的时候还是会遇到很多问题:</p>
<p>第一个问题是数据量很大,我们往往希望把一个有几百万行、几千万行的表数据从一个小型数据库迁移到一个大中型的数据库中,比如把一个单机MySQL数据库中的数据迁移到金仓数据库集群中;或者将上百万的数据迁移到历史数据库中存档给原来的库瘦身。</p>
<p>第二个问题就是不同类型数据库之间进行迁移,比如从MySQL迁移到金仓、从金仓迁移到Oracle,由于源数据库和目标数据库类型不同,它们支持的字段类型、表类型甚至SQL语法都有差异。有些数据库厂商从商业上考虑可能只愿意提供将别的数据库迁移到自家数据库来的功能而不支持反向迁移。</p>
<p>第三个问题是同一种数据库不同版本之间进行数据迁移。按理说数据库不同版本之间应该保持兼容,至少是高版本兼容低版本的,但国产数据库很牛,它不同版本之间的数据类型是可能不兼容的,导致数据无法直接迁移,这个问题让我明白了金仓数据库迁移工具为何使用起来那么复杂。</p>
<p>第四个问题是数据迁移过程可能还伴随数据筛选、数据清洗和转换,这个属于ETL的范畴了,有一些成熟的ETL工具可以使用,但这些工具使用复杂并且不一定免费。</p>
<p>接下来我们自己手搓迁移工具时看怎么解决上面这些问题。</p>
<h1>迁移方案设计</h1>
<ul>
<li>迁移的数据量很大,所以不能读取太多的数据到内存,最好从源数据读取一部分就写入目标数据库一部分数据;</li>
<li>不同种类数据库之间进行数据迁移,由于数据库之间SQL语法、字段类型有差异,所以最好不要直接采用编写SQL语句的方式来实现,用ORM框架可以完美解决这个问题;</li>
<li>数据库不同版本之间的数据迁移,注意采用兼容的数据类型即可,如果不能兼容也有办法;</li>
<li>第四个问题好办了,由于是自己手搓的工具,迁移前后可以自定义自己的处理逻辑,进行数据筛选、清洗和转换工作都不在话下了。</li>
</ul>
<p>根据这个迁移方案,采用SOD框架来实现是很合适的,它的一些特性解决这些问题具有很大优势,下面我们逐个介绍怎么实现。</p>
<h1>准备工作</h1>
<p>首先我们需要明确源数据库和目标数据库的类型、版本,数据库连接信息,要迁移的表和视图数据。比如本文的例子以从金仓数据库迁移到MySQL数据库为例,使用VS先创建一个控制台项目,目标框架选择.NET8,然后项目中添加两个Nuget包,在目文件中添加下面的包引用代码:</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">PackageReference </span><span style="color: rgba(255, 0, 0, 1)">Include</span><span style="color: rgba(0, 0, 255, 1)">="PWMIS.SOD.Kingbase.Provider.Net6V9"</span><span style="color: rgba(255, 0, 0, 1)"> Version</span><span style="color: rgba(0, 0, 255, 1)">="6.0.1"</span> <span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">PackageReference </span><span style="color: rgba(255, 0, 0, 1)">Include</span><span style="color: rgba(0, 0, 255, 1)">="PWMIS.SOD.MySQL.Provider"</span><span style="color: rgba(255, 0, 0, 1)"> Version</span><span style="color: rgba(0, 0, 255, 1)">="6.0.3"</span> <span style="color: rgba(0, 0, 255, 1)">/></span></pre>
</div>
<p>或者使用Nuget包管理工具,查找 PWMIS.SOD 关键字,然后安装SOD框架的金仓数据库访问提供程序和MySQL数据库访问提供程序:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">Install-Package</span><span style="color: rgba(0, 0, 0, 1)"> PWMIS.SOD.Kingbase.Provider
</span><span style="color: rgba(0, 0, 255, 1)">Install-Package</span> PWMIS.SOD.MySQL.Provider</pre>
</div>
<p>然后在解决方案资源管理器选择项目名称,右键菜单“添加-新建项-常规”,然后选择“应用程序配置文件”,添加一个 app.config文件,内容如下:</p>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" id="code_img_closed_4110b682-a97d-422b-b36b-3d4ed471e6f5" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" id="code_img_opened_4110b682-a97d-422b-b36b-3d4ed471e6f5" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_4110b682-a97d-422b-b36b-3d4ed471e6f5" class="cnblogs_code_hide">
<pre><?xml version=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> encoding=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span>?>
<configuration>
<appSettings>
<!--PDF.NET.SOD SQL 日志记录配置(<span style="color: rgba(0, 0, 255, 1)">for</span> <span style="color: rgba(128, 0, 128, 1)">4.0</span><span style="color: rgba(0, 0, 0, 1)">)开始
记录执行的SQL语句,关闭此功能请将SaveCommandLog 设置为False,或者设置DataLogFile 为空;
如果DataLogFile 的路径中包括</span>~<span style="color: rgba(0, 0, 0, 1)">符号,表示SQL日志路径为当前Web应用程序的根目录;
如果DataLogFile 不为空且为有效的路径,当系统执行SQL出现了错误,即使SaveCommandLog 设置为False,会且仅仅记录出错的这些SQL语句;
如果DataLogFile 不为空且为有效的路径,且SaveCommandLog 设置为True,则会记录所有的SQL查询。
在正式生产环境中,如果不需要调试系统,请将SaveCommandLog 设置为False 。
</span>-->
<add key=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">SaveCommandLog</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">True</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<add key=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataLogFile</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Log\SqlLog.txt</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<!--LogExecutedTime 需要记录的时间,如果该值等于0会记录所有查询,否则只记录大于该时间的查询。单位毫秒。-->
<add key=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">LogExecutedTime</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">500</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<!--PDF.NET SQL 日志记录配置 结束-->
</appSettings>
<connectionStrings>
<add name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">SourceDb</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
connectionString</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Server=127.0.0.1;User Id=system;Password=system;Port=54321;Database=mydb;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
providerName</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">PWMIS.DataProvider.Data.Kingbase,PWMIS.KingbaseClient.Net6V9</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<add name =<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">TargetDb</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
connectionString</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">server=127.0.0.1;User Id=root;password=123456;DataBase=mydb;</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
providerName</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">PWMIS.DataProvider.Data.MySQL,PWMIS.MySqlClient</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
</connectionStrings>
</configuration></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>有关如何配置连接字符串的详细内容,请移步框架的Nuget下载页面:NuGet Gallery | PWMIS.SOD 6.0.3</p>
<p>注意金仓数据库访问程序的选择不同版本有点差异,可以移步SOD的金仓数据库Nuget下载页面详细了解:NuGet Gallery | PWMIS.SOD.Kingbase.Provider 6.0.7</p>
<p>到此使用SOD框架开发数据迁移工具的准备工作已经完成,下面正式开始编写实现代码。</p>
<h1> 创建目标数据库</h1>
<p> 数据迁移通常都是目标数据库已经存在的情况下进行的,但这里为什么要强调创建目标数据库呢?这是因为很可能既有的目标数据库的数据表和表字段与源数据库是不兼容的,比如目标数据库的字符编码是UTF8,而源数据库是GB2312,目标表的字段类型是int而源表字段的类型是long,或者表字段都是varchar类型但是源表和目标表该字段的长度却不相同,当然更夸张的是连字段名都可能不相同(字段业务含义是一样的),这些千奇百怪的问题只有你想不到没有你遇不到的。所以最佳办法是由迁移工具自动创建一个目标数据库。</p>
<p>SOD框架的Code First方案可以由实体类创建表,它在第一次连接数据库的时候检查表是否存在,如果不存在才创建表,如果表已经存在则跳过以避免意外更改表结构。实现此过程很简单,只需要继承DbContext即可,比如对于本文的目标数据库,创建一个TargetDbContext类:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> TargetDbContext : DbContext
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span> TargetDbContext () : <span style="color: rgba(0, 0, 255, 1)">base</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">TargetDb</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
{
}
</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">bool</span><span style="color: rgba(0, 0, 0, 1)"> CheckAllTableExists()
{
CheckTableExists</span><UserInfo><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建其它表。。。</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
}</span></pre>
</div>
<p>在上面的代码中,DbContext类的构造函数参数值“TargetDb” 就是app.config中配置的连接名称,重载方法CheckAllTableExists 中 CheckTableExists泛型方法的类型参数UserInfo是一个SOD实体类,它可以根据实体类指定的表名称来创建目标表。这样,当TargetDbContext类型的对象被实例化的时候就会自动创建好迁移数据的目标表了。</p>
<h1>迁移标识字段</h1>
<p>数据库的标识字段是用来唯一标识一行数据的,主键就起到这种作用,我们也使用带自增功能的字段做主键,但自增字段不一定都是主键,本文说的标识字段是数据库的自增标识列,如SQLServer的IDENTITY 列,MySQL 用 AUTO_INCREMENT,Oracle 用 SEQUENCE+触发器或 IDENTITY 列,PostgreSQL和金仓数据库也是用 SEQUENCE 并设置 DEFAULT nextval。</p>
<p>SOD框架的Code First功能可以为各种数据库自动创标识列,只需要实体类设置 Identity="标识字段名"即可,示例代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> UserInfo : EntityBase
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> UserInfo()
{
TableName </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">UserInfo</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
IdentityName </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">标识字段</span>
PrimaryKeys.Add(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">主键</span>
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> ID
{
</span><span style="color: rgba(0, 0, 255, 1)">get</span>{<span style="color: rgba(0, 0, 255, 1)">return</span> getProperty<<span style="color: rgba(0, 0, 255, 1)">int</span>>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);}
</span><span style="color: rgba(0, 0, 255, 1)">set</span>{setProperty(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,value );}
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> Name
{
</span><span style="color: rgba(0, 0, 255, 1)">get</span>{<span style="color: rgba(0, 0, 255, 1)">return</span> getProperty<<span style="color: rgba(0, 0, 255, 1)">string</span>>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Name</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);}
</span><span style="color: rgba(0, 0, 255, 1)">set</span>{setProperty(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Name</span><span style="color: rgba(128, 0, 0, 1)">"</span>,value ,<span style="color: rgba(128, 0, 128, 1)">50</span><span style="color: rgba(0, 0, 0, 1)">);}
}
}</span></pre>
</div>
<p>默认情况下自增字段(IDENTITY / AUTO_INCREMENT / SERIAL)在插入数据的时候不能直接插入值,但在数据迁移的时候,需要将自增字段的值也迁移过去,除非自增字段没有被别的表在逻辑上引用。如果确实需要给自增列塞一个指定值,必须显式关闭/绕过自增字段的这个机制,操作完恢复自增字段的默认行为,否则后续普通方式插入数据会出错。比如对于SQLServer数据库:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 1 允许手动插入自增字段值</span>
<span style="color: rgba(0, 0, 255, 1)">SET</span> <span style="color: rgba(0, 0, 255, 1)">IDENTITY_INSERT</span> dbo.UserInfo <span style="color: rgba(0, 0, 255, 1)">ON</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 2 手动写值(列清单必须显式写出)</span>
<span style="color: rgba(0, 0, 255, 1)">INSERT</span> <span style="color: rgba(0, 0, 255, 1)">INTO</span> dbo.UserInfo (ID Name) <span style="color: rgba(0, 0, 255, 1)">VALUES</span> (<span style="color: rgba(128, 0, 0, 1); font-weight: bold">100</span>, <span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">zhangsan</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)"> 3 恢复自增字段默认行为</span>
<span style="color: rgba(0, 0, 255, 1)">SET</span> <span style="color: rgba(0, 0, 255, 1)">IDENTITY_INSERT</span> dbo.UserInfo <span style="color: rgba(0, 0, 255, 1)">OFF</span>;</pre>
</div>
<p>对于PostgreSQL和金仓数据库的默认模式(PG模式)下,自增字段可以直接插入值,只要插入的值与现有自增字段值不重复即可。SOD框架根据实体类是否设置IdentityName属性来决定插入数据的时候是否插入自增列的值,所以在使用SOD框架迁移数据的时候除了要注意目标数据库对于自增字段的问题,还需要设置IdentityName属性为空值,我们定义一个 IImportable 接口来表示该实体类可以插入自增字段值:</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> IImportable
{
</span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> IgnoreIdentity();
}</span></pre>
</div>
<p>修改前面的实体类,将IdentityName设置为空:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> UserInfo : EntityBase,IImportable
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> public_AlarmsInfo()
{
TableName </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">UserInfo</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
IdentityName </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">标识字段</span>
PrimaryKeys.Add(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">主键</span>
<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> IgnoreIdentity()
{
IdentityName </span>= <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> ID
{
</span><span style="color: rgba(0, 0, 255, 1)">get</span>{<span style="color: rgba(0, 0, 255, 1)">return</span> getProperty<<span style="color: rgba(0, 0, 255, 1)">int</span>>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);}
</span><span style="color: rgba(0, 0, 255, 1)">set</span>{setProperty(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,value );}
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> Name
{
</span><span style="color: rgba(0, 0, 255, 1)">get</span>{<span style="color: rgba(0, 0, 255, 1)">return</span> getProperty<<span style="color: rgba(0, 0, 255, 1)">string</span>>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Name</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);}
</span><span style="color: rgba(0, 0, 255, 1)">set</span>{setProperty(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Name</span><span style="color: rgba(128, 0, 0, 1)">"</span>,value ,<span style="color: rgba(128, 0, 128, 1)">50</span><span style="color: rgba(0, 0, 0, 1)">);}
}
}</span></pre>
</div>
<p>采用这种方式在运行时修改IdentityName 属性值,既可以享受到Code First自动创建目标表的便利,又可以实现插入自增列数据的功能;注意迁移完自增列数据后,需要重置自增列的标识数据到最大的自增列值,这样后续插入数据才不会出问题。对于金仓数据库迁移完成当前表的数据后,可以用下面的方式重置自增列的标识数据:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">entity 是当前正在迁移的表对于的SOD实体类对象</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (targetDb.CurrentDBMSType ==<span style="color: rgba(0, 0, 0, 1)"> PWMIS.Common.DBMSType.Kingbase)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">更新序列值ALTER SEQUENCE equipment_id_seq RESTART WITH 100;</span>
<span style="color: rgba(0, 0, 255, 1)">string</span> tableName =<span style="color: rgba(0, 0, 0, 1)"> entity.GetTableName();
</span><span style="color: rgba(0, 0, 255, 1)">string</span> sql = $<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ALTER SEQUENCE {tableName.ToLower()}_id_seq RESTART WITH {max_id + 1}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
targetDb.ExecuteNonQuery(sql);
}</span></pre>
</div>
<p>另外一种可选方式是在数据库上直接将原来的标识字段修改为普通字段,然后在实体类构造函数里面注释掉 IdentityName 这行字段即可,但用这种方式来进行Code First模式开发无法自动创建自增列,但可以等数据迁移完成后再手动设置自增标识。</p>
<h1> 大数据量查询</h1>
<p> 一次性在内存中加载10万条数据很可能导致进程无法正常运行,而且这种大数据也会导致.NET内存难以有效回收,而大表数据迁移又是很常见的事情,所以最佳方案是数据逐条读取,读一部分写一部分,避免将大量数据读取到内存后再写入,这样可以加快迁移速度。SOD框架的实体类查询支持这种“迭代器查询”,通过调用EntityQuery<T>.QueryEnumerable方法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> DataMigration<T>(AdoHelper sourceDb,AdoHelper targetDb,Action<T> action, <span style="color: rgba(0, 0, 255, 1)">string</span> identityName=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ID</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 0, 255, 1)">where</span> T : EntityBase, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)">()
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">其它代码略</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> oql = OQL.From<T><span style="color: rgba(0, 0, 0, 1)">().END;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> readQuery = EntityQuery<T><span style="color: rgba(0, 0, 0, 1)">.QueryEnumerable(oql, sourceDb);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> insetQuery = <span style="color: rgba(0, 0, 255, 1)">new</span> EntityQuery<T><span style="color: rgba(0, 0, 0, 1)">(targetDb);
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> readQuery)
{
action(item);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">写入数据到目标数据库,代码暂略</span>
<span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">其它代码略</span>
}</pre>
</div>
<p>QueryEnumerable 方法通过DataReader对象循环读取数,每次只返回一个读取的实体类对象,从而避免了一次读取大量数据的问题。</p>
<h1> 数据复制</h1>
<p> 广义的数据复制是将读取的数据写入到目标数据库,但这里说的数据复制是将上面读取的数据复制到一个新的对象里面。虽然理论上可以将从源数据库读取的实体类直接写入到目标数据库,但数据迁移的环境可能比较复杂,比如源数据库和目标数据库是不同类型的数据库,或者虽然类型一样但是版本不一样,或者字段名称不一样甚至字段类型都不完全一样;另外一个原因是SOD实体类的设计与市面上绝大部分ORM都不同,SOD实体类采用值数组的方式存储从数据库读取的原始值,这些值可能携带了数据驱动程序特定的类型信息,而这种类型可能与目标数据库的类型是不兼容的,比如日期类型,MySQl驱动程序有自己的日期子类型,金仓数据库驱动程序也有自己的日期子类型,甚至不同版本的金仓数据库日期子类型还有微小的差异,所以数据迁移的时候最好消除源数据库读取字段的特定的类型信息,直接使用.NET的数据类型,然后让数据库驱动程序根据.NET数据类型转换到目标数据库支持的数据类型。驱动程序数据类型转换的问题比较复杂这里不细究。</p>
<p>针对不同的数据复制场景,SOD有不同的支持方案,最通常的方案是直接调用实体类的MapForm方法做数据映射拷贝:</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">var</span> insetQuery = <span style="color: rgba(0, 0, 255, 1)">new</span> EntityQuery<T><span style="color: rgba(0, 0, 0, 1)">(targetDb);
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> readQuery)
{
T targetEntity </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> T();
targetEntity.MapFrom(item,</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
targetEntity.ResetChanges(</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">其它代码略</span>
}</pre>
</div>
<p>上面代码中MapFrom方法表示从任意一个实例对象中拷贝与当前实体类同名属性的值到当前实体类中,ResetChanges方法强行设置所有属性的修改状态是否修改,SOD框架会根据实体类属性是否修改(是否进行过赋值操作)来决定是否将该属性的值更新或者插入到数据库中。除了调用上面的两个方法,直接使用SOD的扩展方法CopyTo方法也可以实现类似的效果:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> readQuery)
{
T targetEntity </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> T();
item.CopyTo(targetEntity);
}</span></pre>
</div>
<p>但是使用上面的方式都没法复制源数据库表字段的NULL值,这个功能对SOD框架来说很简单:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> readQuery)
{
T targetEntity </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> T();
item.CopyTo(targetEntity);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < item.PropertyValues.Length; i++<span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (item ==<span style="color: rgba(0, 0, 0, 1)"> DBNull.Value)
{
targetEntity.PropertyValues</span>=<span style="color: rgba(0, 0, 0, 1)"> DBNull.Value;
}
}
}</span></pre>
</div>
<p>上面的代码中item是源数据库的实体类对象,targetEntity是目标数据库的实体类对象,SOD的实体类具有索引器功能,可以通过索引访问属性值,也可以通过索引直接修改实体类的属性值,这样就可以为属性设置NULL值。实体类的这种访问方式是绝大部分ORM框架都不支持的功能,这个功能为SOD框架处理数据带来了极大的便利性。</p>
<h1> 批量插入</h1>
<p> 目标数据库的写入是数据迁移过程中最慢的操作,批量插入能大大提高插入操作的性能,很多数据库都有一次性插入多条数据的功能,其中大多支持下面这种方式:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">INSERT</span> <span style="color: rgba(0, 0, 255, 1)">INTO</span><span style="color: rgba(0, 0, 0, 1)"> 表名 (列1, 列2, …)
</span><span style="color: rgba(0, 0, 255, 1)">VALUES</span><span style="color: rgba(0, 0, 0, 1)">
(值1_1, 值1_2, …),
(值2_1, 值2_2, …),
…
(值n_1, 值n_2, …);</span></pre>
</div>
<p>SOD框架对于MySQL和金仓数据库采用了这种方式进行批量插入,对于SQLServer采用了SqlBulkCopy方案。只要支持批量插入,都可以调用QuickInsert方法:</p>
<div class="cnblogs_code">
<pre>InsertListData<T>(List<T> targetList, EntityQuery<T> insetQuery) <span style="color: rgba(0, 0, 255, 1)">where</span> T : EntityBase, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)">()
{
</span><span style="color: rgba(0, 0, 255, 1)">int</span> importedCount=<span style="color: rgba(0, 0, 0, 1)">insetQuery.QuickInsert(targetList);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> importedCount;
}</span></pre>
</div>
<p>如果数据库不支持批量插入,也可以使用EntityQuery对象的Insert重载方法插入一个实体列表对象,内部使用事务等方式优化插入性能。</p>
<p> </p>
<h1>进度信息</h1>
<p> 数据迁移可能耗时比较长,迁移过程中实时显示迁移进度是必要的,我实现了一个ConsoleProcessDisplayer类,效果类似Linux系统中下载文件的命令行进度显示方式,下面直接给出主要代码:</p>
<div class="cnblogs_code"><img src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" id="code_img_closed_4f0b8a70-1b8d-44b3-aaf1-50954f02d596" class="code_img_closed"><img src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" id="code_img_opened_4f0b8a70-1b8d-44b3-aaf1-50954f02d596" class="code_img_opened" style="display: none">
<div id="cnblogs_code_open_4f0b8a70-1b8d-44b3-aaf1-50954f02d596" class="cnblogs_code_hide">
<pre><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 显示处理进度
</span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"></summary></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="readIndex"></span><span style="color: rgba(0, 128, 0, 1)">读取的数据位置</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)"><param name="writeIndex"></span><span style="color: rgba(0, 128, 0, 1)">写入的数据位置</span><span style="color: rgba(128, 128, 128, 1)"></param></span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> DisplayProcessing(<span style="color: rgba(0, 0, 255, 1)">int</span> readIndex,<span style="color: rgba(0, 0, 255, 1)">int</span> writeIndex=<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
{
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(writeIndex==<span style="color: rgba(128, 0, 128, 1)">0</span>) writeIndex =<span style="color: rgba(0, 0, 0, 1)"> readIndex;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (readIndex >=<span style="color: rgba(0, 0, 0, 1)"> recordCount)
{
Console.SetCursorPosition(</span><span style="color: rgba(0, 0, 255, 1)">this</span>.left, <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.top);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> str = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">string</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">=</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">);
Console.Write(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{0}({4}%)</span><span style="color: rgba(128, 0, 0, 1)">"</span>, str, readIndex, writeIndex, recordCount, <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">int</span> currMSec = DateTime.Now.Millisecond / <span style="color: rgba(128, 0, 128, 1)">100</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">0.1秒的显示间隔</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(currMSec!=<span style="color: rgba(0, 0, 0, 1)">lastMSec)
{
lastMSec </span>=<span style="color: rgba(0, 0, 0, 1)"> currMSec;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">显示控制</span>
Console.SetCursorPosition(<span style="color: rgba(0, 0, 255, 1)">this</span>.left, <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.top);
</span><span style="color: rgba(0, 0, 255, 1)">char</span>[] w_arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">char</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> j = <span style="color: rgba(128, 0, 128, 1)">0</span>; j < screenWidth; j++<span style="color: rgba(0, 0, 0, 1)">)
{
w_arr </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">=</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">int</span> p = currentWidth > screenWidth ? currentWidth %<span style="color: rgba(0, 0, 0, 1)"> screenWidth : currentWidth;
w_arr = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (p <<span style="color: rgba(0, 0, 0, 1)"> screenWidth)
w_arr </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (p+<span style="color: rgba(128, 0, 128, 1)">1</span> <<span style="color: rgba(0, 0, 0, 1)"> screenWidth)
w_arr = <span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">string</span> w_str = p >= screenWidth ? <span style="color: rgba(128, 0, 0, 1)">""</span> : <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">string</span>(w_arr,<span style="color: rgba(128, 0, 128, 1)">0</span>,p+<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">double</span> dpCount = writeIndex * <span style="color: rgba(128, 0, 128, 1)">100</span> /<span style="color: rgba(0, 0, 0, 1)"> dcCount;
Console.ForegroundColor </span>=<span style="color: rgba(0, 0, 0, 1)"> ConsoleColor.Green;
Console.Write(w_str);
Console.ForegroundColor</span>=<span style="color: rgba(0, 0, 0, 1)"> ConsoleColor.White;
</span><span style="color: rgba(0, 0, 255, 1)">string</span> w_str2=p>= screenWidth? <span style="color: rgba(128, 0, 0, 1)">""</span>: <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">string</span>(w_arr,p+<span style="color: rgba(128, 0, 128, 1)">1</span>,screenWidth-p-<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">);
Console.Write(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{0}>[{1}/{2}]({3}%)</span><span style="color: rgba(128, 0, 0, 1)">"</span>, w_str2, readIndex, recordCount, dpCount.ToString(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">f2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
}
currentWidth </span>= currentWidth <= screenWidth ? currentWidth + <span style="color: rgba(128, 0, 128, 1)">1</span> : <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>下面是模拟显示进度的代码调用方法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">int</span> recordCount = <span style="color: rgba(128, 0, 128, 1)">3721</span><span style="color: rgba(0, 0, 0, 1)">;
Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">【模拟】开始处理数据:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
ConsoleProcessDisplayer displayer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ConsoleProcessDisplayer(recordCount);
Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">插入记录数:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
displayer.Begin();
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>;i< recordCount; i++<span style="color: rgba(0, 0, 0, 1)">)
{
displayer.DisplayProcessing(i);
System.Threading.Thread.Sleep(</span><span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)">);
}
displayer.DisplayProcessing(recordCount);
displayer.End(); </span></pre>
</div>
<p> </p>
<p>至此已经介绍完成了使用SOD框架实现一个数据迁移工具的主要功能,其它就是一些容错性处理和进度显示控制,以及迁移N个表的方法重复调用,当然如果需要反复进行迁移测试,每次迁移之前可能还需要清理数据,调用DbContext类的TruncateTable泛型方法即可。</p>
<p>如果有朋友对本文的迁移方案感兴趣,可以加群联系我,联系方式参考 http://www.pwmis.com/sqlmap</p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
《SOD框架“企业级”应用数据架构实战》<br><br>
来源:https://www.cnblogs.com/bluedoctor/p/19183530
頁:
[1]