Blog

The Fabulous Free Utility, LINQPad

By Martin Schaeferle | September 13, 2012

Selecting the Entity Framework data context

 

When writing and testing LINQ expressions and queries using LINQ for Entities-or other LINQ variants-you can write a custom application, reference the class library containing your Entity Data Model, and write code that executes the expression and displays the results somehow, perhaps in a console window. All that setup can be a bit tedious when you are working with throwaway LINQ expressions that you tweak, explore, and then delete.

A better option is the fabulous free utility, LINQPad, written by Joseph Albahari. This tool lets you interactively query databases using LINQ expressions you build in its own development environment. The one thing that LINQPad can't easily do is let you explore the Entity Framework and .NET Framework objects used to return data to the application. It does display the direct object type returned with the results, but doesn't let you explore its properties. For more extensive exploration of these objects, the debugging tools in Visual Studio, particularly the object tooltips and Locals window, are invaluable.

Setting Up LINQPad to Use an Entity Data Model Assembly

Before you can write LINQ expressions against an Entity Framework model, you'll have to configure the utility so that it knows which assembly to use and some other basic information to find the correct objects in your Entity Data Model. Follow these instructions for setting up LINQPad to use a pre-built AWLTModel that uses the AdventureWorksLT database. 

Figure 2. Selecting the Entity Framework data context.

Selecting-the-Entity-Framework-data-context

1. Launch LINQPad from the Windows Start menu.

2. Click the Add Connection link in the upper left portion of the LINQPad window, to open the Choose Data Context dialog box.

3. Select the lower radio button, Use a typed data context from your own assembly, and select the Entity Framework item in the list, as shown in Figure 2. This tells LINQPad that you want to use an Entity Framework model rather than a direct connection to a data store. Click Next to move to the next step.

4. In the Entity Framework Custom Assembly Connection dialog box, click the Browse link in the upper right to open an Open File dialog box. Navigate to the AWLT.dll file in this chapter's files, and click the Open button. This causes LINQPad to look inside the assembly for the context and model name, and opens the Choose Custom Type dialog box as shown in Figure 3. Accept the default AWLT.AWLTEntities and AWLTModel selections, and click OK.

Figure 3. Choosing the custom types from the AWLT.dll assembly.
Choosing-the-custom-types-from-the-AWLT.dll-assembly

5. This populates several of the selections in the Entity Framework Custom Assembly Connection dialog box, which is now named the LINQPad Connection dialog box, as shown in Figure 4. Review the selections; you shouldn't have to make any changes here because it reflects the connection information in the assembly, but you can if necessary. Click OK to create the connection.

Figure 4. The fully populatedLINQPad Connection dialog box.

6. LINQPad will gather metadata information from the model assembly and populate the treeview in the upper left portion of the LINQPad window with a list of entities in the assembly. You can expand an entity to view its properties. You're now ready to write LINQ expressions and queries against the AWLT model!

the-fully-populated-LINQPad-connection-dialog-box



Martin Schaeferle

Martin Schaeferle has taught IT professionals nationwide to develop applications using Visual Basic, Microsoft SQL Server, ASP, and XML. He has been a featured speaker at Microsoft Tech-Ed and the Microsoft NCD Channel Summit, and he specializes in developing Visual Basic database applications, COM-based components, and ASP-based Web sites. In addition to writing and presenting technical training content, Martin is also LearnNowOnline's vice president of technology.


This blog entry was originally posted September 13, 2012 by Martin Schaeferle