Code Generation Tool (SqlMetal.exe)
The SqlMetal command-line tool generates code and mapping for the LINQ to SQL component of the .NET Framework. By applying options that appear later in this topic, you can instruct SqlMetal to perform several different actions that include the following:
* From a database, generate source code and mapping attributes or a mapping file.
* From a database, generate an intermediate database markup language (.dbml) file for customization.
* From a .dbml file, generate code and mapping attributes or a mapping file.
The SQLMetal file is included in the Windows SDK that is installed with Visual Studio. By default, the file is located at drive:\Program Files\Microsoft SDKs\Windows\vn.nn\bin. If you do not install Visual Studio, you can also get the SQLMetal file by downloading the Windows SDK.
SQLMetal is an all or nothing process. It includes everything within the database and there is no way to tailor the selection of tables/viewes required. If you do not want select all of the database tables, stored procs etc you will need to use the designer.
SQLMetal is fine for generating the model classes. I am not very fond of the LINQ designer, so typically, I will use the designer in the initial design phase, and later additions or alterations to the datamodel is then done by hand editing the dbml file.
I use this simple command for building my LINQ-to-SQL classes, which I put in a batch file for maximum convenience:
sqlmetal /code:YourCodeFile.designer.cs /namespace:Desired.Namespace YourModel.dbml
The SQLMetal approach works fine for me, and is a reliable way of generating the code. Any handwritten additions to the generated classes, should of course be kept in partial class declarations in a separate code file.
Examples:
Generate a .dbml file that includes extracted SQL metadata:
sqlmetal /server:myserver /database:northwind /dbml:mymeta.dbml
Generate a .dbml file that includes extracted SQL metadata from an .mdf file by using SQL Server Express:
sqlmetal /dbml:mymeta.dbml mydbfile.mdf
Generate a .dbml file that includes extracted SQL metadata from SQL Server Express:
sqlmetal /server:.\sqlexpress /dbml:mymeta.dbml /database:northwind
Generate source code from a .dbml metadata file:
sqlmetal /namespace:nwind /code:nwind.cs /language:csharp mymetal.dbml
Generate source code from SQL metadata directly:
sqlmetal /server:myserver /database:northwind /namespace:nwind /code:nwind.cs /language:csharp
You can extract only functions or only stored procedures or only views using switches:
/views Extracts database views.
/functions Extracts database functions.
/sprocs Extracts stored procedures.
You also have an option to output schema as .dbml file, source code or xml mapping file
/dbml[:file] Sends output as .dbml. Cannot be used with /map option.
/code[:file] Sends output as source code. Cannot be used with /dbml option.
/map[:file] Generates an XML mapping file instead of attributes. Cannot be used with /dbml option.
Miscellaneous output options:
/language:<language> Specifies source code language. Valid <language>: vb, csharp. Default value: Derived from extension on code file name.
/namespace:<name> Specifies namespace of the generated code. Default value: no namespace.
/context:<type> Specifies name of data context class. Default value: Derived from database name.
/entitybase:<type> Specifies the base class of the entity classes in the generated code. Default value: Entities have no base class.
/pluralize Automatically pluralizes or singularizes class and member names. This option is available only in the U.S. English version.
/serialization:<option> Generates serializable classes. Valid <option>: None, Unidirectional. Default value: None.