简体   繁体   English

如何查看LINQ发送到数据库的SQL文本?

[英]How can I see the SQL text that is sent by LINQ to my database?

I have the following repository: 我有以下存储库:

public class GenericRepository<T> : IRepository<T> where T : class
{
    public GenericRepository(DbContext dbContext)
    {
        if (dbContext == null) 
            throw new ArgumentNullException("An instance of DbContext is required to use this repository", "context");
        DbContext = dbContext;
        DbSet = DbContext.Set<T>();
    }

    protected DbContext DbContext { get; set; }

    protected DbSet<T> DbSet { get; set; }

    public virtual IQueryable<T> Find(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Where<T>(predicate);
    }

    public virtual IQueryable<T> GetAll()
    {

        return DbSet;
    }

and a service: 和服务:

    private IRepository<Subject> _subjectsRepository;
    private IRepository<Content> _contentsRepository;

    public ContentService(IRepositoryProvider repositoryProvider)
        : base(repositoryProvider)
    {
        _subjectsRepository = GetStandardRepo<Subject>();
        _contentsRepository = GetStandardRepo<Content>();
    }

    public IList<Content> GetContents(int subjectId, int contentTypeId, int contentStatusId)
    {
        var contents = _contentsRepository.GetAll()
            .Where(a => a.SubjectId == subjectId &&
                   a.ContentTypeId == contentTypeId &&
                   (contentStatusId == 99 ||
                    a.ContentStatusId == contentStatusId))
            .ToList(); 
        return contents;
    }

I would like to find the SQL text that is sent to the database. 我想找到发送到数据库的SQL文本。 I understand I can do this with: 我了解我可以通过以下方式做到这一点:

db.GetCommand(query).CommandText

But could someone help me and tell me where I should put this in my code. 但是有人可以帮助我,告诉我在代码中应该放在哪里。

I would like to find the SQL text that is sent to the database and I understand that I can do this with 我想找到发送到数据库的SQL文本,并且我知道我可以使用

You can use the SQL Server profiler tool that is part of SQL Server Management Studio to see what the server has received and what is executing on it. 您可以使用SQL Server Management Studio 附带SQL Server Profiler工具来查看服务器已收到的内容以及在服务器上执行的内容。 However, this may impact performance, so you shouldn't run it on a production server (unless you know what you are doing). 但是,这可能会影响性能,因此您不应在生产服务器上运行它(除非您知道自己在做什么)。

Other options are using a third party profiler - such as the commercial Entity Framework Profiler from Hibernating Rhinos, or, if using ASP.NET/MVC the open source mini-profiler . 其他选项使用的是第三方分析器-例如Hibernating Rhinos的商业Entity Framework Profiler ,或者,如果使用ASP.NET/MVC,则使用开放源代码微型分析器

You can use ToTraceString() to track the SQL generated by your Linq to Entities queries and dump them into a log. 您可以使用ToTraceString()来跟踪由Linq to Entities查询生成的SQL,并将其转储到日志中。

An extension method like this 这样的扩展方法

public static string ToTraceString<T>(this IQueryable<T> query)
{
    var objQuery = query as ObjectQuery<T>;
    if (objQuery != null)
    {
        return string.Format("{0}{2}{1}{2}{2}", DateTime.Now, objQuery.ToTraceString(), Environment.NewLine);

    }

    return string.Empty;
}

can be called as 可以称为

var sql = _contentsRepository.GetAll().ToTraceString();

I would recommend using LinqPad ( http://www.linqpad.net/ ). 我建议使用LinqPad( http://www.linqpad.net/ )。

With LinqPad you can import your own assembly that contains your DataContext and use your own DAL methods. 使用LinqPad,您可以导入自己的包含DataContext的程序集并使用自己的DAL方法。 See here . 这里

After running the code snippet you can switch being a results view and a SQL view (among others). 运行代码段后,您可以切换为结果视图和SQL视图(以及其他视图)。 This has been the best tool we have found when using EntityFramework. 这是使用EntityFramework时发现的最好的工具。 We also benefit from being able to invoke our DAL more directly, without having to work through a top-level application layer. 我们还受益于能够更直接地调用DAL,而无需遍历顶层应用程序层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM