2

I'm trying to retrieve the raw SQL generated by Entity Framework for the following LINQ query:

pagedItemResults = from firstItem in dbData.Accession
                        join secondItem in pagedRowNumberResults
                        on firstItem.AccessionNumber equals secondItem
                        select new PaginationResultRow
                        {
                            Number = firstItem.AccessionNumber,
                            ID = firstItem.AccessionId,
                            Name = firstItem.AcquisitionType.Name,
                            Description = firstItem.Description
                        };

Although it may be extremely simple and similar to the other answers already out there for previous versions of EF, I've had no luck and found nothing online.. any ideas??

2
  • 1
    Tried running a trace on the SQL instance? Commented Mar 22, 2016 at 15:54
  • 2
    I know on normal Entity framework you could just do pagedItemResults.ToString() and it would output the query string, can you not do that on core? Commented Mar 22, 2016 at 16:00

3 Answers 3

3

You can turn on logging by implementing ILoggerProvider. See details in documentation.

You only need to register the logger with a single context instance. Once you have registered it, it will be used for all other instances of the context in the same AppDomain.

        using (var db = new BloggingContext())
        {
            var serviceProvider = db.GetInfrastructure<IServiceProvider>();
            var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
            loggerFactory.AddProvider(new MyLoggerProvider());
        }

You can also define categories what you want to log.

    private static string[] _categories =
    {
        typeof(Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory).FullName,
        typeof(Microsoft.Data.Entity.Storage.Internal.SqlServerConnection).FullName
    };
Sign up to request clarification or add additional context in comments.

1 Comment

Note, I think he might be using ASP.NET core, according to the documentation it still works but it has slightly different instructions on what you need to do to register the logger.
2

You can log tsql generated to output window by :

Microsoft.Extensions.Logging.Debug

First, get it from Nuget, then in your context, you must define a LoggerFactory.

After that, use it in OnConfiguring in your context.

public static readonly Microsoft.Extensions.Logging.LoggerFactory _loggerFactory =
                    new LoggerFactory(new[] {
                    new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider()
    });

optionsBuilder.UseLoggerFactory(_loggerFactory);

Comments

0

I really like MiniProfiler, see http://miniprofiler.com/. Short of something like this, I would say you'd have to use a profiler on the actual database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.