Friday, September 10, 2010

How to get the generated sql query sending to server by entity framework

This is a sample linq query in EF,
for example, we have 2 tables:
Contents, Tags (one to many, each content has zero or more tags)

then this query is returning all the contents with no tags using left outer join.
(DefaultIfEmpty() only works in EF4)


var tagQuery = select c from m_context.Contents
join t in m_context.Tags on c.ContentID equals t.ContentId into GG
from g in GG.DefaultIfEmpty()
where g.ContentID == null
select c.ContentID;
 
string sql = ((ObjectQuery)tagQuery).ToTraceString();

If we are using EF4.1 and DbContext,
string sql = query.ToString();


Cheers!

1 comments:

lauren said...

Wow I never thought that the way of sending generated sql query to server by entity framework can be so easy and simple.Thanks as your blog solved major issue that I faced.
electronic signature software

Post a Comment