LinqPad for the Entity Framework.
I recently purchased the auto completion addon which is a neat tool for LinqPad. Its pretty cheap and certainly aided in my using LINQ Pad as a scratch pad ala Query Analyzer. For example when dealing with my own Repository and Specification libraries for Entity Framework, I can write and fine tune using scratch pad code along with my libraries directly in LINQ Pad. The benefit is I can verify my queries without any debugging in VS.Net. To use my own libraries that contain the Entity Framework code I can reference those libraries as per below:
and import their namespaces into LinqPad like so:
No I can write code such as:
If you download my library, you can run this code after setting up as per the above.
NorthwindModel.NorthwindEntities context =
new NorthwindModel.NorthwindEntities(
@”Provider=
System.Data.SqlClient;
Provider Connection String=
‘Data Source=boomer;
Initial Catalog=Northwind;
Integrated Security=True;
Connection Timeout=5;
MultipleActiveResultSets=true;’;
Metadata=
res://*/”);
var german_cust_spec =
new Specification<NorthwindModel.Customer>
(c => c.Country == “Germany”);
var italian_cust_spec =
new Specification<NorthwindModel.Customer>
(c => c.Country == “Italy”);
var spec = italian_cust_spec | german_cust_spec;
var query = from cust in context.Customers
.Where(spec.EvalPredicate)
orderby cust.Country
select cust;
query.Dump();
I absolutely love the fact that I have a tactile query analyzer(ish) IDE to test my Entity Framework queries prior to writing them in VS.Net and further to that I don’t miss out on using my custom libraries that contain classes such as the Specification<T> or EntitiesRepository<E, C> as demonstrated below.
NorthwindModel.NorthwindEntities context =
new NorthwindModel.NorthwindEntities(
@”Provider=
System.Data.SqlClient;
Provider Connection String=
‘Data Source=boomer; Initial Catalog=Northwind;
Integrated Security=True; Connection Timeout=5;
MultipleActiveResultSets=true;’;
Metadata=
res://*/”);
var german_cust_spec =
new Specification<NorthwindModel.Customer>(c => c.Country == “Germany”);
var italian_cust_spec =
new Specification<NorthwindModel.Customer>(c => c.Country == “Italy”);
var spec = italian_cust_spec | german_cust_spec;
EntitiesRepository<NorthwindModel.Customer, NorthwindModel.NorthwindEntities>
repos = new
EntitiesRepository<NorthwindModel.Customer, NorthwindModel.NorthwindEntities>
(context);
var repos_query = from cust in repos.All(spec)
select cust;
query.Dump();
If your a LinqPad user you might want to consider buying the LINQPAD auto completion add-on which I get great value out of. I would however love Joseph to make LINQPAD more Entity Framework friendly and provide us the SQL output created by the Entity Framework Provider as it does for LINQ To SQL.
Thanks to Stefan for demonstrating how to use LINQPAD with the Entity Framework in the first instance. Give it a try.
3 comments








