For further information check Scott Hunter's detailed post: Breakdown of Dynamic Data Features.
Moving the data context from the web layer to a lower level will allow you to reuse the LINQ to SQL data context as well as any other logic or wrappers you create around the data model.
Step 1: Create Dynamic Data Website
To begin create a Dynamic Data website.
Step 2: Create Data Layer
The next step is to create a new class library. This project will house your LINQ to SQL data context, the meta-data classes and wrappers for your data access.
Step 3: Add References
When you are using the LINQ to SQL class in the Dynamic Data website you have a pre-established reference setup for System.ComponentModel.DataAnnotations. You must create a reference in the class library in order to have access to the attributes used for the meta-data classes.
Since the DataAnnotations DLL is not loaded in the GAC, you will have to search your system for “System.ComponentModel.DataAnnotations.dll” and copy this file into your project.
Here the DLL is in the Dependencies folder* and then the project is referenced to this DLL.
*Often it’s a good idea to create a Dependencies class library of its own to act as a source control container for any extra DLLs. This will keep you from getting your references tangled up for larger projects.
Step 4: Create Data Context
Next you will create the data context. The process is simple: create a LINQ to SQL file and drag on the tables and views you want to model.
This example only brings in one table...
Step 5: Create the Metadata
The Dynamic Data engine will choose the appropriate field template based off the database type of each field. If you would like override this functionality and use a custom field template then you must add meta-data information on top of the existing models.
As seen on line five, make sure you add the using statement to bring in the DataAnnotations namespace.
After that, creating the meta-data is easy. Simply create a partial class named the same as your model class and add the MetadataType attribute so the Dynamic Data engine knows where to find the meta-data.
The meta-data class exists only to hang the UITemplate attributes off the object properties. You don’t even need to know about the return type of the property at this point.
Normally the Dynamic Data engine will map the CreatedOn field to the DateTime field template. Here we want to re-route the engine to use the Text template so we can verify the application is threaded together correctly.
Step 6: Update the Website
The next step is to update the website so it can see the data layer. Right click on the web project and choose Add Reference.
In order to keep this demo simple we’ll just update one of the stock field templates to verify that we are getting the data context and all the meta-data from the lower-level project. When you setup the meta-data you told a field that would normally map to a DateTime field to go to a Text field template. Since by default both the DateTime and Text field templates just emit the field data on the screen in the read-only view, you’ll update the Text template.
Open the Text field template and simply add the word “text” (or really anything for that matter) to the field template.
The last part of the process is to update the Global.asax
Note that (at least on my machine) IntelliSense would not bring up the data context class name once I typed DataLayer. – so you may have to just type the class name. Once the class was there the coloring came in as expected though.
Finally you can run the website to verify that the CreatedOn field is being rendered using the Text field template.
Obviously the application created in this post is not a true n-tier application, but this shows you what you need to know for you to architect your applications as appropriate.