The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

改用 SqlMetal.exe 建立 LINQ to SQL 的 DBML 檔

我們通常在寫 LINQ to SQL 專案時,都會利用 Visual Studio 2008 內建的 LINQ to SQL DBML Designer 讓我們透過視覺化的介面將資料庫表格、檢視表或預儲程序從 Server Explorer 拖曳到設計視窗中,不過缺點就是當資料庫結構(DB Schema)改變了之後就需要跟著修正,我通常有以下選擇:

  1. 手動加上欄位並自行設定欄位屬性
    • 優點:彈性較大
    • 缺點:手動設定錯誤很容易會出現 Bug,例如:欄位型別設定錯誤或是否 Allow Null 設定錯誤等情況。
  2. 刪除變更的表格,然後重新拖曳那一個表格就好
    • 優點:只需重新拖曳變更的表格就好,重建速度較快。
    • 缺點:當變更的表格較多時,DBML 定義檔中的 XML 結構表格變化比較大,較無法用檔案內容差異分析工具(WinMerge)分析變化的內容。若欄位有做客製化調整的動作,需重新設定一遍,例如:將 Auto Generated Value 設定為 True。
  3. 所有全部刪除,然後全部重新拖曳一遍
    • 優點:全部重建比較乾脆,DBML 定義檔中 XML 結構的表格出現的順序也會跟當初拉的一樣,當不確定到你有哪幾個表格變更的話,可以考慮用這招。
    • 缺點:若欄位有做客製化調整的動作,需重新設定一遍。手動調整過的 Layout 也會全部消失,需要重新調整一次。

這三種方式看情況使用即可。

不過很多人可能不知道在 .NET Framework 3.5 提供的工具中還有包括一個 SqlMetal.exe 指令列工具程式,可以跟透過 LINQ to SQL DBML Designer 所自動產生的 DBML 差不多,不過格式還是不太一樣,所以可能無法交替著使用,如果你原本的專案已經用 LINQ to SQL DBML Designer 建立 *.dbml 檔的話,就不用考慮採用 SqlMetal.exe 工具了,除非你打算調整你現有的 AP 中的 Entity 物件名稱。

我從 SSW Rules to Better LINQ 擷取一段 DBML Designer 與 SqlMetal 的比較表如下:

 DBML Designer 與 SqlMetal 的比較表

從圖表得知,若改用 SqlMetal 工具來建立 LINQ to SQL Entities 會比用 DBML Designer 更有彈性且更自動化,缺點就是較不易客製化。而另一個重點就是 SqlMetal 可以支援 SQL Server Compact,這在 VS2008 中的 DBML Designer 就沒直接支援了,若有使用到 SQL Server Compact 的人,是可以考慮使用 SqlMetal 來產生程式碼。

不過,我跟大家講一個觀念:「若你要採用自動程式碼產生的工具,就要盡量運用自動化的威力,不要拼了命的想客製化自動產生的程式碼,客製化的程式修正應該做在 Base Class 或 Inherit Class 才是正確的用法」。

SqlMetal.exe 工具通常放在 C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin 目錄下,你只要先開啟 Visual Studio 2008 Command Prompt,然後輸入 sqlmetal 指令就可以執行了。

D:\Program Files\Microsoft Visual Studio 9.0\VC>sqlmetal

直接輸入 sqlmetal 指令不加上任何參數就會列出完整的說明:

Microsoft (R) Database Mapping Generator 2008 version 1.00.30729
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

SqlMetal [options] [<input file>]

  Generates code and mapping for the LINQ to SQL component of the .NET framework. SqlMetal can:
  - Generate source code and mapping attributes or a mapping file from a database.
  - Generate an intermediate dbml file for customization from the database.
  - Generate code and mapping attributes or mapping file from a dbml file.

Options:
  /server:<name>             Database server name.
  /database:<name>           Database catalog on server.
  /user:<name>               Login user ID (default: use Windows Authentication).
  /password:<password>       Login password (default: use Windows Authentication).
  /conn:<connection string>  Database connection string. Cannot be used with /server, /database, /user or /password options.
  /timeout:<seconds>         Timeout value to use when SqlMetal accesses the database (default: 0 which means infinite).

  /views                     Extract database views.
  /functions                 Extract database functions.
  /sprocs                    Extract stored procedures.

  /dbml[:file]               Output as dbml. Cannot be used with /map option.
  /code[:file]               Output as source code. Cannot be used with /dbml option.
  /map[:file]                Generate mapping file, not attributes. Cannot be used with /dbml option.

  /language:<language>       Language for source code: VB or C# (default: derived from extension on code file name).
  /namespace:<name>          Namespace of generated code (default: no namespace).
  /context:<type>            Name of data context class (default: derived from database name).
  /entitybase:<type>         Base class of entity classes in the generated code(default: entities have no base class).
  /pluralize                 Automatically pluralize or singularize class and member names using English language rules.
  /serialization:<option>    Generate serializable classes: None or Unidirectional (default: None).
  /provider:<type>           Provider type: SQLCompact, SQL2000, SQL2005, or SQL2008. (default: provider is determined at run time).

  <input file>               May be a SqlExpress mdf file, a SqlCE sdf file, ora dbml intermediate file.

Create code from SqlServer:
  SqlMetal /server:myserver /database:northwind /code:nwind.cs /namespace:nwind

Generate intermediate dbml file from SqlServer:
  SqlMetal /server:myserver /database:northwind /dbml:northwind.dbml /namespace:nwind

Generate code with external mapping from dbml:
  SqlMetal /code:nwind.cs /map:nwind.map northwind.dbml

Generate dbml from a SqlCE sdf file:
  SqlMetal /dbml:northwind.dbml northwind.sdf

Generate dbml from SqlExpress local server:
  SqlMetal /server:.\sqlexpress /database:northwind /dbml:northwind.dbml

Generate dbml by using a connection string in the command line:
  SqlMetal /conn:"server='myserver'; database='northwind'" /dbml:northwind.dbml

這些說明就包括許多使用範例了,我想我應該不用贅述了。

相關連結

留言評論