using System.Data.Objects
var results = db.tb_product.Where(s =>s.ID == 2160)
var sql = (results as ObjectQuery<tb_product>).ToTraceString()
在EF4.1中,可以直接 使用 ToString()
using (var context = new MyDbContext())
{
var people = from p in context.People
where p.PersonID >100
select p
string sql = people.ToString()
}
在EF 4.1中,我们可以直接调用DbQuery<>的ToString()方法得到所生成的SQL。using (var context = new MyDbContext())
{
var people = from p in context.People
where p.PersonID > 100
select p
string sql = people.ToString()
}
所生成的SQL是:
SELECT
[Extent1].[PersonID] AS [PersonID],
[Extent1].[Name] AS [Name]
FROM [dbo].[People] AS [Extent1]
WHERE [Extent1].[PersonID] > 100
大 家应该已经猜到,这里的ToString()方法其实也就是调用了ObjectQuery<>的ToTraceString()方法。 DbQuery<>.ToString() ==>System.Data.Entity.Internal.Linq.InternalQuery<>.ToString()方法,此方法 在.NET Reflector得到的实现是这样的:
public override string ToString()
{
return this._objectQuery.ToTraceString()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)