Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Entity Framework 6 & SQL Server Compact (6)–Entity Framework Reverse POCO Code First Generator

DZone's Guide to

Entity Framework 6 & SQL Server Compact (6)–Entity Framework Reverse POCO Code First Generator

· Database Zone ·
Free Resource

Slow SQL Server? These SentryOne resources share tips and tricks for not only troubleshooting SQL Server performance issues, but also preventing them before they hit your production environment.

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:

<connectionStrings>
   <add name="MyDbContext" 
    providerName="System.Data.SqlServerCe.4.0"
    connectionString="Data Source=C:\data\mydb.sdf" />
</connectionStrings>

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.

Database monitoring tools letting you down? See how SentryOne empowers Enterprises to go faster.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}