WCF, Entity Framework and N-Tier Solutions – Part 1

Because of a new project at work, I have recently had the opportunity to do my first real WCF based project. It has been a great deal of fun and a learning experience as well. While doing the project of course, I have been doing a great deal of reading and web research. Over the course of the project, two main issues have come up.

1. How do I use WCF with the Entity Framework (Referred to hereafter as EF)
2. How do I use WCF in an n-tier project

So, with the caveat that I still have a lot to learn, so far here is what I have found on the subject.


#1 – The Entity Framework

Due to project requirements coming from the client, the Entity Framework needed to be the basis for my database access and it needed to be exposed via WCF so that multiple front end systems could use it. Initially, I was thinking that I could just expose my Entity Framework classes via WCF and be done with it, however in trying that I ran into several problems. Among other things, the Entity Framework would naturally expose a great deal more information that I am comfortable with. This is an issue for two reasons. The first is a matter of security, since the EF basically gives you the ability to perform almost any imagineable operation on your database. Depending on the operations that you want available through publicly viewable web services, that might not be a wise thing to do. Also, because of the sheer volume of methods exposed, you can end up with a lot more information going back and forth over the wire, which can affect performance.


#2 – N-Tier Architecture

I don’t think that this would be completely impossible with EF, but at the same time none of the tutorials that I found really demonstrated a good way of doing it. Most of them just wanted to show exactly how WCF and/or EF work using a basic example and were not concerned with showing an implementation of a full project using n-tier architecture and working together (which is perfectly understandable for the purposes of a tutorial). But when you are trying to use EF and WCF for the first time, even if you understand how each works individually, it is not easy to make them work together in an n-tier setting.



The solution to this problem, turned out to be something called “POCOs”. This term was new to me and understanding the concept took some additional reading. But simply put, POCOs stands for “Plain Old CLR Object”. In essence, what this solution involves is downloading and installing a template in Visual Studio that will connect to your EF .edmx file and create simple classes that map to your Entity Framework model and hence to your database. Once the template is created, you can edit and save it, which will in turn rerender your simple classes with the properties that you have specified. At first, I could not wrap my brain around how this worked, but I understand it now and have found it to be an excellent solution.


I did eventually get this working and I have written another post that explains the process in detail. It is very long and for that I apologize, but if you are trying to accomplish the same thing, perhaps too much detail is better than not enough. To read that post, click here.

Tags: , , ,

3 Responses to “WCF, Entity Framework and N-Tier Solutions – Part 1

  1. Tjeuten says:

    Nice article, I’m kinda in the same situation like you. I wanted to build an application using WCF for client/server comms, and EF on the server side to interact with the database.

    I ended up going with STE’s (Self Tracking Entities), but I’m very interested in how you implemented something similar with POCO’s, so I’m very much looking forward to your second part ! :-)

  2. […] application. If you would like to read it that post first (it might be a good idea) you can click here. Since it was posted, I have noticed that a few people visit the post every day and they are […]

  3. […] you’ve read my two previous posts on the subject, (Part 1 and Part 2) you will know that I promised to do a part 3 to this series that will show how to wire […]