Entity Framework

Entity Framework has haunted me from some time. The initial release suffered from no lack of mockery from the web as a whole. Rightfully so. I was always in the camp that liked LINQ to SQL pretty well, and I was sad to see it go. But like every ORM type framework out there both EF and LINQ had their advantages and disadvantages. I even spent a little time with NHibernate (via Fluent NHibernate) and found that to occupy the same space. On the outset it seemed like the greatest thing… upon further investigation it became less than its initial impression.

That said, I didn’t set out to create a rant here. The frustrations I’ve experienced all came together the other day as I was trying to wrap my head around some issues I was having with EF4. Specifically, attempting to come up with a good solution for dealing with the scope of EF contexts in a WPF client application. At the same time I was worrying about that bit of the overall solution architecture I ran into the bug in my previous post. It so happens it was just enough added stress to start the reaction and cause a juvenile rant.

The Guid issue was, is course, a simple workaround (but it still irks me). After a couple days of reflection and a couple trials I’m pretty comfortable with my solution to the context scope as well. In fact, the solution to that made me realize one of the big disconnects I’ve been having with ORMs in general. I realized I’ve been interacting with ORMs (EF in particular) in largely the same manner as I have every other data retrieval framework and api for years. That’s kind of an embarrassing thing to admit. Regardless, here I am and it’s just now that the light bulb tuned on.

In essence, I’ve been treating EF more or less like a set of utility functions. I’m not sure I can easily explain it off the cuff, nor do I really feel like writing it all down tonight. I’ve got a few other things on my mind. So I’m going to try and sum it up in a couple sentences.

I started treating ObjectContexts as if they were the database itself, not just a set of helpers that get me out of writing simple sql statements. It’s just like another file. It seems stupidly simple, I know, and I’m probably the last guy on the planet to grasp that. It is pretty apparently when you’re watching one of the canned demos that some MS shill is doing, on some sample app that you know will never approximate something that would be useful in the real world. Not always so when you’re digging into something concreted (or looking at a Ria services demo).

Your ObjectContext is your database. Treat it carefully and be concerned about accessing it consistently. Now go be productive.