LINQ to SQL 数据库同步器






4.96/5 (14投票s)
一个开源工具,可将您的数据库结构与 LINQ to SQL 模型同步。
引言
LINQ to SQL 数据库同步器 (csdb.exe) 是一个工具,它提供从 LINQ to SQL 模型进行数据库结构同步。它支持创建新数据库和更新现有数据库。需要注意的是,此工具永远不会删除任何数据或结构;它只会添加缺少的表、列和索引,并修改现有列。
主要优势
- 一键式 O/R 模型同步
- 更好的团队生产力,简化团队成员之间的数据库更改
- 更简单的产品安装和升级,无需复杂的 SQL 脚本
- 维护一个模型而不是两个模型更简单
背景
我之前提到的 DataContext.CreateDatabase
方法仅支持创建新数据库。这意味着,为了修改现有数据库上的 LINQ to SQL 模型,必须手动在数据库上应用结构更改,或者完全删除数据库并再次调用 CreateDatabase
方法。当团队中的每个成员都有自己的数据库用于测试时,这个缺点会大大减慢团队的速度。其他成员添加的任何需要数据库结构更改的功能,都会迫使其他成员在自己的数据库上运行各种脚本或执行手动更改。对于需要用新产品版本更新的生产服务器也是如此。
前面提到的 DataContext.CreateDatabase
方法使用 SQL 脚本来创建数据库及其内部的表。而这个工具使用 SQL Server Management Objects (SMO) 库。它分析 LINQ to SQL 模型,将其与现有数据库结构进行比较,最后将所有必需的更改应用于数据库结构。
代码内部
我们基本上有两个模型需要同步 - 一个是 LINQ to SQL 模型,另一个是数据库模型。所以,让我们从加载 LINQ to SQL 模型开始
var asm = Assembly.LoadFrom(Options.AssemblyFile); //Load an assembly file
var type = asm.GetType(Options.TypeName, true); //Find the DataContext class
//using reflection
var model = new AttributeMappingSource().GetModel(type); //Load the LINQ to SQL mapping
//model from the specified type
现在,我们将使用 SMO 和我们拥有的连接字符串加载数据库模型。
var sb = new SqlConnectionStringBuilder(ConnectionString); //Parse the
//connection string
var server = new Server(sb.DataSource); //Connect to the
//database server
var db = server.Databases[sb.InitialCatalog]; //Get the database
现在我们拥有了所有需要的数据,接下来只需遍历 LINQ to SQL 模型,并定位相应的数据库对象。如果它们不存在,我们就直接创建它们;否则,我们就验证它们的定义。
foreach (MetaTable table4 in model.GetTables())
{
string schemaName;
string tableName;
ParseFullTableName(mt.TableName, out schemaName, out tableName); //Split the schema
//and table name
var table = Database.Tables[tableName, schemaName]; //Find the table
if (table == null) //If the table
//doesn't exist
{
table = new Table(Database, tableName, schemaName); //Create the table
Database.Tables.Add(table);
}
//Now we can synchronize the table columns...
}
Using the Code
此工具是一个简单的命令行工具 (*.exe),只需指定一个程序集和一个 LINQ to SQL DataContext
类的类型名称并运行它,您的数据库结构将由该模型同步。
必备组件
此工具使用 SQL Server Management Objects (SMO),其最新版本可以在 Microsoft SQL Server 2008 Feature Pack 下载页面找到,但以下是直接链接
用法
此工具的使用方式与任何标准命令行工具一样,语法如下
csdb.exe /assembly:[AssemblyFile] /type:[TypeName] /autocreate
/autoupdate /cs:[ConnectionString] /dbfilename:[DatabaseFilename]
/assembly:[AssemblyFile] The assembly filename that contains the LINQ to SQL
(http://msdn.microsoft.com/en-us/library/bb425822.aspx)
DataContext class, e.g.: bin\debug\MyApp.exe
/type:[TypeName] Optional, The type name of the LINQ to SQL
(http://msdn.microsoft.com/en-us/library/bb425822.aspx)
DataContext class, e.g.: MyApp.MyDataContext.
if not provided,
any class that inherits from the DataContext
class will be processed.
/autocreate When specified, the tool will create the database
if it doesn't exist.
/autoupdate When specified, the tool will update the existing
database structure.
/cs:[ConnectionString] Optional, The connection string of the database
to be synchronized, if not provided, the default
connection string will be used if it exists
in your settings.
@[Arguments Filename] Read the command line arguments from an
external text file.
示例
此语句将使用 MyApp.exe 程序集中的 MyApp.MyDataContext 类来同步(创建和更新)本地计算机 SqlExpress 实例中的 MyDb
数据库。
csdb.exe /assembly:"bin\debug\MyApp.exe" /type:"MyApp.MyDataContext"
/autocreate /autoupdate /cs:"Data Source=.\SQLEXPRESS;Initial
Catalog=MyDb;Integrated Security=True"
建议
- 指定
/autocreate
和/autoupdate
选项以实现最大自动化。 - 创建一个批处理文件来执行此工具,并将其包含在您的项目中。
- 在开发早期阶段,请将此批处理文件作为项目文件中的生成后事件运行。
运行示例
源代码包含一个示例项目,其中包含众所周知的 Northwind 数据库的 LINQ to SQL 模型。要运行示例
- 安装先决条件 - SQLSysClrTypes.msi,SharedManagementObjects.msi
- 打开解决方案并生成它
- 运行 Samples\SyncMyNorthwindDb.bat 批处理文件来创建数据库
- 运行
Samples
项目
要更改 LINQ to SQL 模型并同步数据库
- 通过使用 Visual Studio 修改 MyNorthwind.dbml 文件来修改 LINQ to SQL 模型,您可以添加列、添加表、更改列的数据类型、允许列为
null
等... - 生成
Samples
项目以反映您的更改。 - 运行 SyncMyNorthwindDb.bat 批处理文件以同步您的数据库。
关注点
我认为该工具解决了使用 LINQ to SQL Framework 时的一个基本需求。我们在 CodeRun 在开发和部署中都广泛使用它。这样,修改数据库就像添加属性一样简单。
历史
- 2009 年 6 月 2 日:初始版本