Thursday, December 3, 2009

Entity Framework combined with Class Diagrams

You liked the Class Diagrams in Visual Studio ? Use them with Entity Framework !!

Here is how :

1/ Use your favorite Developper's environment to create your Entity models (and the dependencies and inheritance as required). Let us assume that your file is called BusinessModel.edmx :

2/ Save the model. This will generate automatically the classes associated with the entities,

3/ Create a Class Diagram, call it for instance

4/ Open the Class View panel and drill down to find your classes, then Drag and Drop your classes either one by one or by selecting many of them onto the diagram.

5/ By default, the diagram is read only, and all properties / items are grayed out.

This is because it was generated by EF4;
Create a new file, called for instance 'BusinessModelParialClasses.cs'
that is going to extend the partial EF generated classes stored in the BusinessModel.Designer.cs

6/ You can extend the classes within the 'BusinessModelParialClasses.cs' file, for example, add the two following methods :
public bool TurnPages(int NumberOfPagesToTurn) ...
public bool TurnChapter(int NumberOfChapterToTurn) ...

on the Book class.

7/ You can notice that now the Class Diagram of the Book Class is not fully read only anymore (the elements generated by EF4 are still read only), and offers the 'Add' option like you are used to:

8/ Obviously, you can access all the Class Details pan properties, like you normally would.

9/ That's it ! let's go back to your Edmx file, where you can "Generate Database Script from Model". And here you go, nearly zero line of code !!!

How lazy I am, more over, a snap shoot of the display and copy/paste will create a nice documentation.
Well ideally, the best would be to generate a dynamic Silverlight 3.0 image within a web document hosted on a Web server ;-) Sounds like SharePoint 2010 ??

Note: This was used with Visual Studio 2010 Beta + EntityFramework 4

1 comment:

  1. If I have the diagram of Edmx , How Can I get the classes Diagram?


Note: Only a member of this blog may post a comment.