• Mocking your Entity Framework data context and testing it in .NET MVC

    I explained in my previous post Do we need the repository pattern? why you might want to mock your Entity Framework context rather than using the repository pattern. In this post I’ll show you how to do that and how to test your .NET MVC controllers with a fake data context.

    The way you do this varies slightly depending on whether you're using code first or database first. I’ll cover both in this post.

    Using Code First in Entity Framework 4

    My implementation of code first is based heavily on the blog post by Rowan Miller.

    A mockable data context (EF Code First)

    When you create your code …

  • Reviewing my data access layer using the Entity Framework Profiler

    Inspired by Oren Eini’s series of blog post code reviews and hearing him talk on This Developers Life Life I decided to review the data access of my latest application using the Entity Framework Profiler.

    This is the second application I’ve used the Entity Framework for, and I’ve found it a joy to work with. I’d heard the arguments against ORMs that said they can’t produce good SQL, but I’d also heard the argument that ORMs are made by very smart people and they can probably make better SQL than most people. What I hadn’t heard till I heard Oren’s interview was that a smart ORM will still …