A brief introduction to Entity Framework Power Tools

Microsoft introduced Code First in Entity Framework 4.1 and that was a revolution in ORM world and since then, Entity Framework is truly much more than a pure ORM.

Although Microsoft has attempted to keep programmers away from direct designing database but I think there is a long way to achieve this goal. At the moment many developers still prefer to first design database and tables and then use the of magic of the Code First.

But the problem is that when you first design database, you are in charge of creating Code First Classes and metadatas. Defining metadata with Data Annotation or Fluent API can take a considerable time and absolutely is a tedious work!

Recently I've got familiar to a great plugin for Visual Studio 2010 & 2012 named Entity Framework Power Tools. This plugin has written by Entity Framework Team at Microsoft. Even though plugin is still in Beta stage but I found it very stable and functional.

Using this plugin you can generate all the Code First classes and related metadata from the database. Metadata is generated by Fluent API method and the result is clean and exciting.

Now let's examine the tools.

First download the Entity Framework Power Tools plugin and install it. Then Open visual studio and create a new Class Library project and name it DataLayer. Keep in mind that project .NET version should be 4.0 or greater.

Right click on the DataLayer project name and you can see there is a new menu option named "Entity Framework" and it has two sub menus as shown in image below.



Choose "Reverse Engineer Code First" option and then Connection Properties dialog will be appeared.



Select your database and hit OK button. Now dependent on the size of the database, it may take a few seconds to few minutes to geterare classes. After classes are generated, you can see there is a folder named "Models" which contains database POCO classes and inside the folder is another folder named "Mapping" which contains metadata classes (Fluent API method).

In below you can see an image from the my Solution Explorer:



And here is my Country class, generated from the Country table in Models folder.



And also here is metadata class for Country table named CountryMap in Mapping folder.



That is cool! Is not that? In addition, there is a class named DatabaseName+Context in Models folder which is your Context class. That is for example if your database name is TestDb, then the Context class name is TestDbContext.

Tip:

Remember the project should contains the references System.Data.Entity and System.ComponentModel.DataAnnotations; and if needed add some namaspaces in some classes to compile it successfully.

And there is also more fun!

Right click on the Context class (Here is TestDbContext) and you can see there is a few new options which some of them explained below:

  1. View Entity Data Model ReadOnly: If you choose this option, visual studio generates a diagram including all classes and relationships! in fact it generates an Edmx file that as the name implies, is readonly and does not affect your code.

  2. View Entity Data Model XML: Visual Studio generates a XML representation of classes and relationships.

  3. View Entity Data Model DDL SQL: Visual Studio generates T-SQL codes for creating database structure.

 A preview of View Entity Data Model ReadOnly:



 A preview of View Entity Data Model XML:



 A preview of View Entity Data Model DDL SQLL:



In the end, it seems Entity Framework Power Tools is a necessary tool for every Entity Framework developer.

No Comments