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??
Advertisement
Answer
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 };