您的位置:首页 > 数据库

监测EF和Linq to SQL产生的SQL

2013-01-09 21:47 302 查看
简单记录下,原文在:http://www.dotnetjalps.com/2012/12/Where-I-can-find-SQL-Generated-by-Entity-framework.html

1、监测EF产生的SQL

using System;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Data;

namespace EntityframeworkSQL
{
class Program
{
static void Main(string[] args)
{

using (CustomerEntities customerEntities = new CustomerEntities())
{
var customerNames = from c in customerEntities.Customers
select c.CustomerName;
string sql = ((System.Data.Objects.ObjectQuery)customerNames).ToTraceString();

Console.WriteLine(sql);

}

}
}
}


2、监测Linq to SQL产生的SQL

using System;
using System.Linq;
using System.Data;

namespace LinqToSQL
{
class Program
{
static void Main(string[] args)
{
using (CustomerDataContext customerContext = new CustomerDataContext())
{
var customerNames = from c in customerContext.Customers
select c.CustomerName;
string sql = customerContext.GetCommand(customerNames).CommandText;

Console.WriteLine(sql);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: