Partager via


SampleEdmxCodeGenerator sources

 

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

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 'https://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

Comments

  • Anonymous
    January 25, 2008
    Thank you so much for posting these helpful articles!  I would also encourage your team to implement a plug-in architecture for the CodeGenerators, so that the overall process is easier for end-developers (no need to go into the registry... it is 2008, after all. ;)). If a plugin system was used, then all developers would have to do is create a plugin assembly (by decorating the assembly with a specified attribute), compile, and place the resulting assembly within a specified plugin directory.  Simple. :) I actually have put together an assembly that does just this.  I'll have to clean it up and have someone take a look at it.  (you have inspired me!)

  • Anonymous
    January 26, 2008
    Today we went live with something very dear to me: The Entity Framework Toolkits &amp; Extensions . This

  • Anonymous
    May 13, 2008
    Entity Framework FAQ Version 0.4 – 5/13/2008 New in this version of the FAQ… · Major Update for VS 2008

  • Anonymous
    May 17, 2008
    I am developing a multi language web site using asp.net, linq to entities (or perhaps linq to sql). I have to handle some data in a multi lingual manner. For example I have a News entity in the conceptual model and I'd like to map it to (News, NewsLocal pair) tables in such a way to be able to do following things:

  1. News n = News.CreateNews(id21, "en-US"); a. n.CreatedOn = DateTime.Now; // must go to News.CreatedOn field b. n.ValidTo = n.CreatedOn.AddDays(7); c. n.Title = "News Test Title"; // must go to NewsLocal.Title field d. n.Body = "News Test Body"; e. db.AddToNews(n); f. db.SaveChanges();
  2. var q = from n db.News where n.CultureId == "en-US" && n.ValidTo <= DateTime.Now select n;
  3. Some similar approach for update and delete … Any solution, advice or alternative is needed and welcomed! Following are table creation scripts: USE [ECMS] GO /****** Object:  Table [dbo].[News]    Script Date: 05/16/2008 13:23:04 / SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[News]( [Id] varchar COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL, [CreatedOn] [datetime] NULL, [ValidTo] [datetime] NULL, CONSTRAINT [PK_News] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF USE [ECMS] GO / Object:  Table [dbo].[NewsLocal]    Script Date: 05/16/2008 13:24:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[NewsLocal]( [NewsId] varchar COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL, [CultureId] varchar COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL, [Title] varchar COLLATE SQL_Latin1_General_CP1256_CI_AS NULL, [Body] varchar COLLATE SQL_Latin1_General_CP1256_CI_AS NULL, CONSTRAINT [PK_NewsLocal] PRIMARY KEY CLUSTERED ( [NewsId] ASC, [CultureId] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[NewsLocal]  WITH CHECK ADD  CONSTRAINT [FK_NewsLocal_News] FOREIGN KEY([NewsId]) REFERENCES [dbo].[News] ([Id]) ON UPDATE CASCADE ON DELETE CASCADE GO ALTER TABLE [dbo].[NewsLocal] CHECK CONSTRAINT [FK_NewsLocal_News] j_chamanara@yahoo.com
  • Anonymous
    May 30, 2008
    Thank you so much for the post!  If I can get this working then I'll be able to use my own base class, which is a must for any good business rules. I'm having a problem though: If I run the custom tool, it says "Cannot find custom tool 'SampleEdmxCodeGenerator" But it's definately in the gac, and I didn't change anything else about it and I did run the reg file. Any suggestions?

  • Anonymous
    June 11, 2008
    The Entity Framework enables developers to reason about and write queries in terms of the EDM model rather than the logical schema of tables, joins, foreign keys, and so on. Many enterprise systems have multiple applications/databases with varying degrees

  • Anonymous
    August 09, 2008
    Part of the Entity Framework FAQ . 2. Architecture and Patterns 2.1. Does Entity Framework have support