3.2 Generate Entity Classes
We’re now ready to use SPMetal to generate entity
classes for our extranet sample. Open a command prompt, change the
current directory to C:\Code\Chapter14\ LinqSampleApplication, and then
execute the following command (note that this command should be entered
on a single line, not on two lines as shown here):
%SPROOT%\bin\SPMetal.exe /web:http://localhost/Chapter14/CustomerTemplate / →
code:HireSample.cs
Tip
Define a system variable to point to SharePoint
root; it saves a whole load of time instead of typing C:\Program
Files\Common Files\Microsoft Shared\Web Server Extensions\14\. Instead
you can simply type %SPROOT% as a shortcut.
SPMetal will create a new file named HireSample.cs
in the project directory for our LinqSampleApplication. To add it into
the project, select Add Existing Item from the Project menu, and then
select the file named HireSample.cs.
Have a look at the code in HireSample.cs. You’ll notice that each class is declared using the partial
keyword. This allows you to extend the functionality of the
auto-generated classes using additional files so that if the classes
are automatically regenerated using SPMetal, any customizations are not
lost.
Figure 3 shows the objects that are defined in HireSample.cs. You’ll notice that a HireSampleDataContext class that’s derived from DataContext
has been defined and has properties corresponding to each of the three
lists: AssetNotes, HireContracts, and OnHireAssets. Additionally, there
are two classes derived from AssetNote and OnHireAsset, named AssetNotesAssetNote and OnHireAssetsOnHireAsset.
These derived classes contain the additional lookup values that we
added to define the relationships between the content types. Since the
lookup values were not added to the content type for reasons mentioned
earlier, a new derived class exists containing only the lookup field.