WCF Data Service with Entity Framework 4.3.1 POCO’s

WCF Data Services is Microsoft’s framework for exposing a data model using a rich RESTful OData API. It integrates seamlessly with Microsoft’s Entity Framework.

The Entity Framework is Microsoft’s Flagship Object Relational Mapper (ORM). It has grown into a rich tool. Recently, support for generating POCO’s instead of the default Entities has been added. The default Entities derive from Entity Frameworks EntityObject class. POCO entity classes have no base class and no dependency on the Entity Framework. This has two big advantages:

  1. Increased testability of Entity classes.
  2. Decreased dependency on the Entity Framework.

Testability is a must-have feature when working on large enterprise applications. It’s important ensure the application is working as expected as changes are made, especially when using an iterative sofware process with frequent releases. Tests are the best way to verify an application is working as expected.

Access to dependencies such as an external library, Database or web service should be minimized to safe guard against future changes. A system which has high coupling with an external dependency is more likely to have a change in one tier cause changes in other tiers of code. This creates a brittle system.

Entity Framework POCO’s are a step in the right direction to build a robust system!

Entity Framework POCO’s Step-By-Step

Below are the steps to creating Entity Framework POCO’s and exposing them with WCF Data Services.

1. Install Entity Framework 4.3.1 NuGet Package.

2. Add New Item -> ADO.NET Entity Data Model -> Model.edmx

3. Add tables to Model.edmx.

In this example I use the User, Contact and ContactGroup tables from before.

4. Open Model.edmx in the designer view. Right Click -> Add Code Generation Item… -> Online Templates -> EF 4.X POCO Entity Generator for C#

5. Create OData Service.

6. Override CreateDatasource

You need to override the CreateDataSource method.

public class WcfDataService1 : DataService<ObjectContext>
    public static void InitializeService(DataServiceConfiguration config)
        config.SetEntitySetAccessRule("Contacts", EntitySetRights.AllRead);
        config.SetEntitySetAccessRule("ContactGroups", EntitySetRights.AllRead);
        config.SetEntitySetAccessRule("Users", EntitySetRights.AllRead);

        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        config.UseVerboseErrors = true;

    protected override ObjectContext CreateDataSource()
        var ctx = new servicestackEntities();
        var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
        objectContext.ContextOptions.ProxyCreationEnabled = false;
        return objectContext;



This entry was posted in Architecture and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s