Entity Framework Reverse POCO Generator for Visual Studio: Introduction


Scope

In this article we'll see the basic usage of an object module which can be integrated with Visual Studio, and definitely useful to work, through Entity Framework ORM, in Code-First mode. That model is called EntityFramework Reverse POCO Generator, and it's downloadable from Visual Studio Templates section, more precisely Data section. We'll see more information about that later. 

Introduction

As is widely known, the new Entity Framework release, 7.x or Core, doesn't support Database-First mode, which gives a easier approach when the developer needs to create a data model from an existing database, but also for EDMX models management, since it allows the graphical representation of derived entities and a more fluid management of the update procedure of the model itself. Operations like table adding, deleting or updating are easier indeed in EF6. Code-First, by its parts, possesses a greater flexibility and is preferable in case of complex projects (which can need the data model delocalization on an external DLL).

Despite it's name, Code-First, is perfectly suited for working on already existent databases, in fact it allows also a partial derivation of entities, allowing to define in an incomplete way the table fields, reducing our models to what we really need for the particular situation of our programs. Obviously that doesn't apply to all those conditions which could impede the correct referentiality or data congruency. The main problem, however, remains, and it's still the fact that - having to work on an already created database - the developer will still have to define the data context, and all the classes which represents the tables (existent or simply to be used). That could be a trivial task if we are speaking about small databases, but in case of an entity number which exceeds the hundred, in which every table possesses many dozens of fields, the task increases in difficulty, and it's more time-consuming.

In Database-First case, the solution will be easy: we'll open the EDMX model, simply updating it by selecting the entities to add or remove, letting to EF the task of remodeling the classes. In Code-First, beyond the manual operations we must do nonetheless, it can be useful to have a tool which helps us in automate POCO (Plain-Old CLR object) entities creation, in order to make a little bit easier and faster (and gradual, too) the implementation of those classes we will need in our projects. And here comes in handy a great solution made by Simon Hughes

EntityFramework Reverse POCO Generator

As described by the Visual Studio Gallery page of the project, EntityFramework Reverse POCO Generator allows the reverse engineering of an existent database, generating POCO classes, Configuration Mappings, and DbContext. It supports the SQL Server interfacing, in its desktop version and in Compact 4.0 too. In the following paragraphs we'll see some basic precautions to use it, and we will do a test on a reduced database. This won't give the reader the opportunity to fully appreciate the tool advantages, so the reader is invited to do some further tests on more important databases, in order to realize the potentiality of that instrument. 

Download and installation

From our solution, after installing Entity Framework (6.x o 7.x version), we'll proceed in adding a new item. Go to the «Data» section, then click «Click here to go online and find templates». 

In all probability, EntityFramework Reverse POCO Generator will be the first result of the Template list. If not, it will be sufficient to search for it typing parts of its name into the window's search bar.

Let's set a name for the T4 file, then proceed in adding it to our solution by clicking «Add» button. That will launch the VSIX Installer to download and install the tool for the first time. After the installation completes, Visual Studio will advise us to restart the IDE.

A first view

EntityFramework Reverse POCO Generator will include several files into our solution, The ones on which we will work directly will be template file (Database1.tt in the article) and - secondly, and only if required - on the class file linked to it (Database1.cs in the article). That file will contain, after the correct template configuration, the DbContext and the entire entities list, with their mappings. Please note that, the first time we will visualize the T4 files, the IDE warns us about a null reference error. That happens because the template need a particular connection string to work properly, and that string must be present in the App.config file.

That string can be removed from the App.config file when the model is successfully generated, and it must be included again only if we wish to refresh the model itself. That's an important aspect, since in the case of a model delocalization into a DLL, separated from the main project, we won't likely will our DLL to be dependent from duplicate connection string, to be found elsewhere than the main project. More on this will follow. 

Configuration

Let's open our App.config file, to type in it a connection string, which will resemble the following:

<connectionStrings>
   <add name="MyDbContext"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True;
        Application Name=MyApp" />
</connectionStrings>

Obviously, there is the need of customizing it with the real data from your database. It's important here to assign to the connection the same name which appears into the ConnectionStringName parameter from the T4 file, which is a value the developer can modify.

If now we will save the template file, the data context will be created with all the database objects in it. So, it's useful to list some of the main basic settings the template will process, to allow a more parametrized elaboration regarding what the template will create.

Parameter Description
ConnectionStringName  Name of the connection string the template will use
DbContextName Name to assing to the created DbContext
GenerateSeparateFiles
Allows to create a single class file for each derived entity
Inflector.PluralizationService
Controls the pluralization services for the derived entities
IncludeStoredProcedures Allows specifying if stored procedures must be derived from the model
TableFilterInclude
Regular expression to specify which tables to include
TableFilterExclude Regular expression to specify which tables to exclude 
StoredProcedureFilterInclude
Regular expression to specify which stored procedures to include
StoredProcedureFilterExclude
Regular expression to specify which stored procedures to exclude

In order to be helped in creating the regular expression for the object to include into a data model, the reader can download a graphics tool from the following link: https://efreversepocoui.codeplex.com/

Apart from the mentioned settings, please note the great number of possibilities, which the reader can test according to its needs: the template is in fact well documented, and it's simple to understand. When we are satisfied with the specified settings, we can proceed in saving it. If there aren't errors, it will proceed in creating the indicated entities. 

Operations like that, since the indication of the connection string, to the saving of the template, must be repeated in all those cases which needs a new model derivation. After a data model derivation, the developer can proceed as usual, using the so created classes for the known operations about the context management. Further notions about the Code-First paradigm use was given into the article  «Entity Framework Introduction using C#, part II - Code-First and Migrations», to which the reader is invited to refer for insights. 

Download

EntityFramework Reverse POCO Generator is downloadable as standalone package, integrable in Visual Studio, from the following link: https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838


Entity Framework insights

Other Languages

The present article is available in the following localizations: