As a follow up to my previous article on Customizing Code Generation in the ADO.NET Entity Designer, here are the sources and instructions for SampleEdmxCodeGenerator.

SampleEdmxCodeGenerator is not intended for production use; instead, it demonstrates custom tool extensibility via SingleFileGenerator, code generation APIs, code generation events and EDM metadata APIs. The sample is also somewhat incomplete on error handling and has not been tuned for performance, stress, etc.

SampleEdmxCodeGenerator doesn’t have that nice double-click error handling mechanism that EntityModelCodeGenerator does. So double-clicking on an error reported by SampleEdmxCodeGenerator will typically open the EDMX file in XML Editor and may not always navigate to the correct line and column.

That said, my goal here was to provide you with enough insight into how the ADO.NET Entity Designer generates code in Visual Studio and hopefully give you a head start with some sample source code.

I’d love to hear from you if you found this useful in a specific code generation scenario you had.

Building and deploying

NOTE: You don’t need to install the Visual Studio 2008 SDK to use this sample.

Build

1.       Download and unzip SampleEdmxCodeGenerator.zip to a folder on your hard disk.

2.       Start Visual Studio 2008 and open SampleEdmxCodeGenerator.sln

3.       Build the solution to get SampleEdmxCodeGenerator.dll in the project output directory

Register

Building the project does not register SampleEdmxCodeGenerator with Visual Studio; you should do this manually. If you plan to register and unregister often, it may be a good idea to create a batch file with these steps to make this easier.

1.       Exit Visual Studio 2008 if it is running

2.       Start a Visual Studio 2008 Command Prompt available from Start à All Programs à  Visual Studio 2008 à  Visual Studio Tools
NOTE: Run as Administrator if running on Windows Vista.

3.       Type the following command:
gacutil /i <full path to SampleEdmxCodeGenerator.dll>

4.       Next, type the following command to register SampleEdmxCodeGenerator with Visual Studio 2008:
reg import <full path to RegisterWithVS.reg>
NOTE: RegisterWithVS.reg is in the same folder as the SampleEdmxCodeGenerator sources.

Unregister

Unregistering SampleEdmxCodeGenerator is pretty much the reverse of the registration operation. If you plan to register and unregister often, it may be a good idea to create a batch file with these steps to make this easier.

1.       Exit Visual Studio 2008 if it is running

2.       Start a Visual Studio 2008 Command Prompt available from Start à All Programs à  Visual Studio 2008 à  Visual Studio Tools
NOTE: Run as Administrator if running on Windows Vista.

3.       Type the following command:
gacutil /u SampleEdmxCodeGenerator

4.       Next, type the following commands exactly as they appear to unregister SampleEdmxCodeGenerator with Visual Studio 2008:

reg delete HKLM\SOFTWARE\Microsoft\VisualStudio\9.0\CLSID\{322EB3BE-19FB-4B6D-9370-1A6474C60D00}

reg delete HKLM\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}\SampleEdmxCodeGenerator

reg delete HKLM\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\{164B10B9-B200-11D0-8C61-00A0C91E29D5}\SampleEdmxCodeGenerator

Test

The SampleEdmxCodeGenerator sources folder has file called SampleModel.edmx that you can use for basic sniff test purposes.

Process an EDMX file with SampleEdmxCodeGenerator

As you probably noticed by now, an EDMX file’s “Custom Tool” property is set to EntityModelCodeGenerator. Making Visual Studio use SampleEdmxCodeGenerator is pretty simple: select the EDMX file in Solution Explorer and type SampleEdmxCodeGenerator as the “Custom Tool” property value.

Visual Studio will immediately run SampleEdmxCodeGenerator to process the EDMX file. You can force Visual Studio to call SampleEdmxCodeGenerator to process the EDMX contents by right-clicking the EDMX file in Solution Explorer and choosing “Run Custom Tool”.

EntityModelCodeGenerator and SampleEdmxCodeGenerator can live side-by-side with each other. For example, you can have 2 EDMX files in the same project with the “Custom Tool” for one set to EntityModelCodeGenerator and other set to SampleEdmxCodeGenerator.

Add and edit CSDL annotations

Since the ADO.NET Entity Designer has no support to visually add or edit CSDL annotations you’ll need to open the EDMX file in XML Editor to do this. This is easy enough to do as follows:

1.       Right-click on the EDMX file in Solution Explorer and choose “Open With…”

2.       Click on “XML Editor” in the “Open With” dialog box

3.       Click OK

4.       The EDMX file opens in XML Editor instead of the ADO.NET Entity Designer

5.       If you prefer, you can reformat the XML document to make it more legible by pressing Ctrl+A and clicking on the “Reformat Selection” button on the XML Editor toolbar

While the ADO.NET Entity Designer doesn’t support it, you could easily imagine a Visual Studio Addin that shows CSDL annotations in a nice GUI and lets you edit them outside of the designer.

When an EDMX file with CSDL annotations is opened in XML Editor, Visual Studio will show information messages saying something like:

Could not find schema information for the attribute 'http://tempuri.org/SampleAnnotations:ClrAttributes'.

These messages can be safely ignored but if you really care then you can create an XSD for your custom namespace and copy the XSD into %vsinstalldir%\Xml\Schemas. Doing this not only gets rid of the messages but you also get intellisense when you type CSDL annotations in XML Editor.

The parsing logic in SampleEdmxCodeGenerator is pretty simplistic and expects multiple CLR attributes to be separated by ‘;’ and only supports string and bool attribute parameters. You could easily extend the sample to work with more complicated constructs.

As soon as you make a change to the EDMX file, Visual Studio will call SampleEdmxCodeGenerator to process the EDMX contents which will immediately generate classes with the appropriate CLR attributes.

Open an EDMX file with CSDL annotations in the designer

The ADO.NET Entity Designer has no support to visually add or edit CSDL annotations. However, the designer preserves any existing CSDL annotations in the EDMX file and the file is rendered properly.

Thus, it is possible to open an EDMX file in the designer even when it contains CSDL annotations and even when it’s “Custom Tool” property is set to SampleEdmxCodeGenerator. One side-effect of this is classes for any new entities created via the designer will be generated by SampleEdmxCodeGenerator. This may be ok as long as you are aware this is happening under the covers.

When an EDMX file with CSDL annotations is opened in the ADO.NET Entity Designer, Visual Studio will show information messages saying it could not find schema information like it did when the EDMX file was opened in XML Editor.

 Sanjay Nagamangalam
Program Manager, ADO.NET