The Entity Framework Reverse POCO Code First Generator is a customizable T4 template, that reverse engineers an existing database and generates Entity Framework Code First POCO classes, Configuration mappings and DbContext It is available directly in Visual Studio via the Tools, Extensions and Updates menu item. It can be used with VS 2010 or later, and the latest version supports Entity Framework 6 and both SQL Server and SQL Server Compact (both 3.5 and 4.0).
This T4 template is similar to the Entity Framework Power Tools “Reverse Engineer Code First” feature, but much more versatile and flexible, and constantly updated by the developer. You can see how this project compares with the Power Tools feature here.
To get started using the template with SQL Server Compact, follow these simple steps:
Install the relevant Entity Framework NuGet package:
- EntityFramework.SqlServerCompact.PrivateConfig (EF 6.0.2 with SQL CE 4.0)
- EntityFramework.SqlServerCompact (EF 6.1 when released with SQL CE 4.0)
- EntityFramework.SqlServerCompact.Legacy (EF 6.1 when released with SQL CE 3.5 – currently available as beta)
Add a connection string to your app.config/web.config:
connectionString="Data Source=C:datamydb.sdf" />
In Visual Studio, right click project and select “add – new item”.
Select Online, and search for “reverse poco”.
Select the “EntityFramework Reverse POCO Code First Generator” template
Give the file a name, such as Database.tt and click Add.
Edit the Database.tt file and specify the connection string name as “MyDbContext” which matches your name in app.config.
Save the Database.tt file, which will now generate the Database.cs file.
There are many options you can use to customise the generated code. All of these settings are in the Database.tt files with comments attached.