If you are doing any work with Entity Framework and custom WCF services in EFv1, everything works great. As soon as you jump to EFv4, you may find yourself getting odd errors that you can’t seem to catch. The problem is almost always has something to do with the new lazy loading feature in Entity Framework 4. With Entity Framework 1, you didn’t have lazy loading so this problem didn’t surface. Assume I have a Person entity and an Address entity where there is a one-to-many relationship between Person and Address (Person has many Addresses). In Entity Framework 1 (or in EFv4 with lazy loading turned off), I would have to load the Address data by hand by either using the Include or Load Method: var people = context.People.Include("Addresses");
or
people.Addresses.Load();
Lazy loading works when the first time the Person.Addresses collection is accessed:
1: var people = context.People.ToList();
2:
3: // only person data is currently in memory
4:
5: foreach(var person in people)
6: {
7: // EF determines that no Address data has been loaded and lazy loads
8: int count = person.Addresses.Count();
9: }
10:
Lazy loading has the useful (and sometimes not useful) feature of fetching data when requested. It can make your life easier or it can make it a big pain.
So what does this have to do with WCF? One word: Serialization.
When you need to pass data over the wire with WCF, the data contract is serialized into either XML or binary depending on the binding you are using. Well, if I am using lazy loading, the Person entity gets serialized and during that process, the Addresses collection is accessed. When that happens, the Address data is lazy loaded. Then the Address is serialized, and the Person property is accessed, and then also serialized and then the Addresses collection is accessed. Now the second time through, lazy loading doesn’t kick in, but you can see the infinite loop caused by this process. This is a problem with any serialization, but I personally found it trying to use WCF.
The fix for this is to simply turn off lazy Loading. This can be done at each call by using context options:
context.ContextOptions.LazyLoadingEnabled = false;
Turning lazy loading off will now allow your classes to be serialized properly. Note, this is if you are using the standard Entity Framework classes. If you are using POCO, you will have to do something slightly different.
With POCO, the Entity Framework will create proxy classes by default that allow things like lazy loading to work with POCO. This proxy basically creates a proxy object that is a full Entity Framework object that sits between the context and the POCO object. When using POCO with WCF (or any serialization) just turning off lazy loading doesn’t cut it. You have to turn off the proxy creation to ensure that your classes will serialize properly:
context.ContextOptions.ProxyCreationEnabled = false;
The nice thing is that you can do this on a call-by-call basis. If you use a new context for each set of operations (which you should) then you can turn either lazy loading or proxy creation on and off as needed.