简体   繁体   中英

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. 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

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. 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 .

You can use ToTraceString() to track the SQL generated by your Linq to Entities queries and dump them into a log.

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/ ).

With LinqPad you can import your own assembly that contains your DataContext and use your own DAL methods. See here .

After running the code snippet you can switch being a results view and a SQL view (among others). This has been the best tool we have found when using EntityFramework. We also benefit from being able to invoke our DAL more directly, without having to work through a top-level application layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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