Using LinqPad to Query & Troubleshoot Your Custom Entity Framework DbContext, Unit of Work & Repository Pattern

I received a couple of questions on how to use LinqPad to run queries through the Unit of Work and Repository pattern implementations from my previous blog. With that being said, here’s a blog post on how to accomplish this since I wasn’t able to find any good documentation on this either.

For anyone that has not had the opportunity to work with LinqPad, this is definite a must have, as part of your development toolkit. It’s great to hash out some quick code, PoC’s, and troubleshooting EF queries. LinqPad is awesome, especially when working with a large solution, so that you can avoid lengthy build times and compilations just to test out a small block of code. You can quickly and easily test and verify your code and/or queries first in LinqPad, before building it into your actual project.

Download the source code (!5536&authkey=!APbwDGYDnBjHJPE) from my previous blog that uses EF5 and make sure everything compiles.

Note: Although in the original blog post I used the Alpha release of EF6, I’ve ran into some intermittent issues with it using LinqPad, so for purposes of this post we’ll stick the release version, EF5.

Launch Linqpad and perform the following steps:

  1. Add a new Connection
    6-11-2013 5-01-17 PM

  2. Add a Typed Data Context Connection
    6-11-2013 5-03-33 PM

  3. Browse to the Data.dll in the Web\Bin directory
    6-11-2013 5-07-31 PM

  4. Browse to the Web.config file of the Web project
    6-11-2013 5-09-26 PM

  5. Verify that the Northwind DbContext has been reflected on by LinqPad
    6-11-2013 5-10-21 PM

  6. Navigate to Query Properties dialog window
    6-11-2013 5-12-01 PM
  7. Add all the following references so that we can use the Unit of Work and Repository assemblies
    6-11-2013 5-13-48 PM

  8. NuGet Entity Framework Version 5.0.0
    6-11-2013 5-15-06 PM

  9. Verify that your Query Properties has the following references
    6-11-2013 5-16-12 PM
  10. Add the following Namespace Imports
    6-11-2013 5-17-09 PM

  11. Change your Query Language to C# Program and select NorthwindContext in Data.dll as your Connection.
    6-11-2013 5-18-59 PM
  12. Code up a LinqPad query using the Unit of Work and Repository implementation and out the results using the LinqPad’s Dump() extension method.
    void Main()
    	int? page= null;
    	var pageNumber = page ?? 1;
    	const int pageSize = 20;
    	int totalCustomerCount;
    	var unitOfWork = new UnitOfWork();
    	var customers =
    			.Include(i => i.Orders)
    			.OrderBy(q => q
    				.OrderBy(c => c.ContactName)
    				.ThenBy(c => c.CompanyName))
    			.Filter(q => q.ContactName != null)
    			.GetPage(pageNumber, pageSize, out totalCustomerCount);	
  13. You should see your results returned through our Unit of Work and Repository implementation
    6-11-2013 5-24-15 PM

  14. As well as the generated SQL from EF
    6-11-2013 5-25-24 PM

Happy Coding…! 🙂

2 thoughts on “Using LinqPad to Query & Troubleshoot Your Custom Entity Framework DbContext, Unit of Work & Repository Pattern

  1. Pingback: What I learned from two PluralSight Videos | John Marsing's Law/Religion Blog

Comments are closed.