2. Create the XML Mapping File
Visual Studio 2008 provides a LINQ-to-SQL intellisense schema located at Program Files\Microsoft Visual Studio 9.0\Xml\Schemas\LinqToSqlMapping.xsd. The schema contains a namespace of http://schemas.microsoft.com/linqtosql/mapping/2007 in it that can be used in your custom LINQ-to-SQL XML mapping files to get intellisense as you create your XML mappings. Start by creating an XML file in your Visual Studio 2008 project. Once the file is created add a Database element that defines the http://schemas.microsoft.com/linqtosql/mapping/2007 namespace on it (see the XML code below for an example). Doing this gives you intellisense for the XML mapping file as you type in additional tags. Within the Database element add one or more Table elements. Each Table element contains child Column elements that define how the individual table columns map to class properties. In addition to defining table mappings, stored procedures can also be defined using a Function tag.
Here's an example of mapping the Customer class shown earlier to the Customer table in AdventureWorksLT. Notice that the intellisense xsd schema namespace mentioned earlier is defined on the root element in the mapping file. While the namespace isn't required, having intellisense definitely speeds up the process of creating the mapping file.
<?xml version="1.0" encoding="utf-8" ?> <Database Name="AdventureWorksLT" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007"> <Table Name="SalesLT.Customer" Member="Model.Customer"> <Type Name="Model.Customer"> <Column Name="CustomerID" Member="CustomerID" IsDbGenerated="true" IsPrimaryKey="true" /> <Column Name="Title" Member="Title" /> <Column Name="FirstName" Member="FirstName" /> <Column Name="LastName" Member="LastName" /> <Column Name="ModifiedDate" Member="ModifiedDate" /> </Type> </Table> <Function Name="dbo.ap_GetCustomerByLastName" Method="GetCustomerByLastName"> <Parameter Name="lastNameLetter" Parameter="LastNameLetter" /> <ElementType Name="Model.Customer" /> </Function> </Database>
Looking through the file you'll see that the Model.Customer class is mapped to the SalesLT.Customer table and that each field is mapped as appropriate. The primary key in the table is defined using the IsPrimaryKey attribute along with the IsDbGenerated attribute. The XML mapping file also includes a reference to a stored procedure named ap_GetCustomerByLastName which looks like the following:
CREATE PROCEDURE dbo.ap_GetCustomerByLastName ( @LastNameLetter char(1) ) AS BEGIN SELECT CustomerID, Title, FirstName, LastName, ModifiedDate FROM SalesLT.Customer WHERE LastName LIKE @LastNameLetter + '%' END
The mapping file maps the stored procedure to a custom method named GetCustomerByLastName which accepts a single parameter named lastNameLetter. Data returned from the stored procedure call is automatically mapped to the Model.Customer class using the mapping file's ElementType element.
The SqlMetal.exe command-line tool can be used to automatically generate the XML mappings for a database quickly and easily (it can generate code as well if desired). Although it won't handle mapping the database fields to your custom data entity classes, it can be used to generate initial XML mapping code that can be modified quickly. To generate the mapping file you can run the following command using the Visual Studio 2008 command prompt. This command generates an XML mapping file for the entire AdventureWorksLT database and includes functions and stored procedures in the output:
sqlmetal.exe /server:YourDBServer /database:AdventureWorksLT /dbml:AdventureWorksLT.dbml /functions /sprocs /namespace:Data