Entity framework 4 – Part 1 – CTP 2 – Clean code with POCO entities

Part 2 – Relationships between non public members
Part 3 – Adding pluralization support

Ok. I have spent some hours today, fiddling a bit with the new Entity Framework (inspired by Julie Lerman at Öredev 2009). I wanted to be as POCO as possible without as much fuzz as possible. One thing that got in my way is the possibility of registering an entityset with a non generic implementation of ContextBuilder.RegisterSet. If I instead could have called: RegisterSet(type); I could easily have auto-registered my entities using reflection. I have tried to get a answer from Microsoft in this issue, the only response I got was from Alex James and the result was that he wrote an Tip about it at his blog.

The result of my fiddling could be downloaded here.

Note! You need to tweak the connection string in the App.Config and the code will try to create the database dynamically.

The code has two version. One where I work against a custom EntityStore and one directly against the Entity Framework.The reason for why I have created the EntityStore is just to decouple the client from needing explicit references to the Entity Framework infrastructure and being able to create another implementation that e.g uses NHibernate instead.

Consuming using Entity Store

var userAccount = new UserAccount
    Username = "sc",
    Password = "ilovesnow",
    Email = "santa@christmas.com"

IEntityStore es = new EfChristmasEntityStore("Test");


var match = es.Query<UserAccount>().Where(uac => uac.Email == userAccount.Email).Take(1).Single();


Consuming using tweaked Entity Framework

//You can also consume it like this (but then you need references to System.Data.Entity and Microsoft.Data.Entity.CTP):

var builder = new ObjectContextBuilder<ObjectContext>("Test");

builder.RegisterEntity(new UserAccountConfig());

using (var ctx = builder.CreateContext())
    if (!ctx.DatabaseExists())


    var match = ctx.EntitySet<UserAccount>().Where(uac => uac.Email == userAccount.Email).Take(1).Single();


2 thoughts on “Entity framework 4 – Part 1 – CTP 2 – Clean code with POCO entities

  1. Pingback: Entity framework 4 – Relationships between non-public members « Daniel Wertheim

  2. Pingback: Entity framework – Part 3 – Adding pluralization support to a non strongly typed ObjectContext. « Daniel Wertheim

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