Tuesday, October 18, 2016

Using LINQPad to test and query with Entity Framework

If you want to easily test out your Entity Framework assemblies without writing a separate Console application or creating a separate Visual Studio project, the best way to do this is to use LINQPad!

I have been using LINQPad for several years for all of my C# development needs including Entity Framework, but I have never shared a Blog post about how to do this in the past.

If you go to the LINQPad site, you can find a brief article on how to work with Entity Framework here: http://www.linqpad.net/EntityFramework.aspx

However, this article is not quite comprehensive and leaves out a number of pieces, therefore, I thought I would share a step-by-step article for those working with Entity Framework.

  1. Open up LINQPad and in the left hand navigation, you will see an option to "Add connection"
  2. You will then be presented with a dialog to select your Entity Framework connection.  For most developers using Entity Framework v. 4.1 or above, you will choose the Entity Framework (DbContext) option.
  3. Once you have chosen your desired Entity Framework connection, you will be presented with another dialog which allows you to choose your Entity Framework assembly
  4. Once you choose your Entity Framework assembly, you will be required to choose the corresponding DbContext for your Entity Framework assembly
  5. Next, you will be required to specify a connection string for your Entity Framework assembly. For most developers, you will choose the option for "Via the parameterless constructor" which will subsequently require you to select the database connection string from an App.config file
  6. Then, you will have to choose the path to the App.config file (which will be usually named something like MyAssembly.dll.config)
  7. Lastly, you will need to choose a name for your Entity Framework connection.
  8. Once all necessary information has been entered, you can "Test" your connection.
  9. If the test is successful, you can click on "OK" for the Test dialog and then click on "OK" again so that your Entity Framework connection is added to LINQPad!
  10. Now, to use this connection in your Entity Framework queries, you can click on the newly created connection hyperlink for "Use <my>connection" or you can select your connection from the "Connection" dropdownlist.  
  11. After this is complete, you are all set to begin writing queries against your Entity Framework assembly in the LINQPad Query window!  Woo hoo!

No comments:

Post a Comment