Database Reference
In-Depth Information
Solution
To use Model First and create an entity with a TimeStamp property, do the following:
1.
Find the T4 Template that is used to generate the DDL for Model First. This file is located in
Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\
Entity Framework Tools\DBGen\SSDLToSQL10.tt . Copy this file, and rename this copy to
SSDLToSQL10Recipe7.tt . Place the copy in the same folder as the original.
Replace the line that starts with [<#=Id(prop.Name)#>] with the code in Listing 14-14.
We'll use this modified T4 Template to generate the DDL for our database.
2.
Listing 14-14. Replace the Line in the T4 Template with This Line
[<#=Id(prop.Name)#>]
<#if (string.Compare(prop.Name,"TimeStamp",true) == 0)
{#>TIMESTAMP<#} else { #><#=prop.ToStoreType()#><#} #>
<#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#>
<#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>
3.
Add a new ADO.NET Entity Data Model to your project. Start with an Empty Model.
4.
Right-click the design surface, and select Add Entity. Name this new entity PhonePlan .
Change the Key Property name to PhonePlanId. Click OK.
5.
Add Scalar properties for Minutes, Cost, and TimeStamp. Change the type for the Minutes
property to Int32. Change the type for the Cost property to Decimal.
6.
Change the type of the TimeStamp property to Binary. Change its StoreGeneratedPattern
to Computed. Change the Concurrency Mode to Fixed.
7.
Right-click the design surface, and view the Properties. Change the DDL Generation
Template to SSDLToSQL10Recipe7.tt . This is the template that you modified in step 2.
Change the Database Schema Name to Chapter14 .
8.
Right-click the design surface, and click Generate Database from Model. Select the
connection and click Next. The generated DDL is shown in the dialog box. Listing 14-15
shows an extract from the generated DDL that creates the PhonePlan table. Click Finish to
complete the generation.
Listing 14-15. The DDL that creates the PhonePlan table
-- Creating table 'PhonePlans'
CREATE TABLE [Chapter14].[PhonePlans] (
[PhonePlanId] int IDENTITY(1,1) NOT NULL,
[Minutes] int NOT NULL,
[Cost] decimal(18,0) NOT NULL,
[TimeStamp] TIMESTAMP NOT NULL
);
GO
How It Works
The TimeStamp data type is not a portable type. Not all database vendors support it. It is unlikely that this type will be
supported at the conceptual layer in future versions of Entity Framework. However, future versions will likely improve
the user experience in selecting or modifying the appropriate T4 template that will generate the DDL.
 
Search WWH ::




Custom Search