3.3 Controlling Entity Creation Using a Parameters File
To save any confusion, it would be nice to rename a
few of the entities, particularly the derived entities containing the
additional columns. While it’s perfectly possible to do this manually
in code, the next time SPMetal is used to regenerate the entities, all
changes will be lost and any code that refers to the old entity names
will fail to compile. Instead, SPMetal allows for the use of a
parameters file.
In Visual Studio, add a new XML file named SPMetalParameters.xml. Insert the following XML into the file:
<?xml version="1.0" encoding="utf-8" ?>
<Web AccessModifier="Internal"
xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal">
<List Name="Asset Notes" Type="AssetNote">
<ContentType Name="Asset Note" Class="AssetNote" />
</List>
<List Name="On-Hire Assets" Type="OnHireAsset">
<ContentType Name="On-Hire Asset" Class="OnHireAsset" />
</List>
<ContentType Name="Asset Note" Class="BaseAssetNote"/>
<ContentType Name="On-Hire Asset" Class="BaseOnHireAsset"/>
</Web>
Save the file, and then rerun SPMetal using the following command line (as before, this command should be entered on one line):
%SPROOT%\bin\spmetal.exe /web:http://localhost/Chapter14/ →
customertemplate /code:HireSample.cs /parameters:SPMetalParameters.xml
If you now examine HireSample.cs, you’ll notice that the objects have been renamed as specified in the parameters file. Figure 4 shows the new object model.
3.4 Incorporate a Build Script to Regenerate Entities Automatically
Rather than having to rerun SPMetal manually each
time your data structure changes, you can incorporate a build script
into your Visual Studio project that calls SPMetal automatically each
time the project is built.
In Visual Studio, in the Project menu, select
LinqSampleApplication Properties. In the Properties pane, select Build
Events, and then click the Edit Pre-Build button. In the dialog that
appears, enter the following script (in one continuous line; line
breaks are added here for readability):
%SPROOT%\bin\spmetal.exe /web:http://localhost/Chapter14/customertemplate→
/code:"$(ProjectDir)HireSample.cs"→
/parameters:"$(ProjectDir)SPMetalParameters.xml"
Build the project. You’ll notice that when
the build has completed, you’re notified that HireSample.cs has been
modified. This confirms that the prebuild script has automatically
regenerated the file as part of the build process.