Over a million developers have joined DZone.

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

To stay on top of the changing nature of the data connectivity world and to help enterprises navigate these changes, download this whitepaper from Progress Data Direct that explores the results of the 2016 Data Connectivity Outlook survey.

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.

Turn Data Into a Powerful Asset, Not an Obstacle with Democratize Your Data, a Progress Data Direct whitepaper that explains how to provide data access for your users anywhere, anytime and from any source.

Topics:

Published at DZone with permission of Erik Ejlskov Jensen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}