How to initialize ORM Profiler with ASP.NET MVC DI services.AddDbContext<DataContext>(options =>)

Posts   
 
    
John Smith
User
Posts: 6
Joined: 24-Oct-2010
# Posted on: 15-Jul-2019 06:28:47   

First - thank you for your help!

I'm working through the tutorial in the APress book: Pro Entity Framework Coe 2 for ASAP.NET Core MVC without any problems.

I'd like to use OMR Profiler to view the SQL EF is sending to SQL Server, so if the profiler is meant to be used with the LLBLGen Pro ORM product only just let me know.

--

The ORM Profiler docs are very clear and explicit, so the problem and help I'm asking for clearly has to do with pilot error not the product.

I simply don't understand where I need to apply the ORM Pofiler initialization code and was hoping you could point me in the right direction.

I've included the entire Startup.cs class for reference at the bottom.

Here is how I connect to the database currently:

string conString = Configuration["ConnectionStrings:DefaultConnection"]; services.AddDbContext<DataContext>(options => { options.EnableSensitiveDataLogging(); options.UseSqlServer(conString, providerOptions => providerOptions.CommandTimeout(60)); });

How do I initialize the ORM Profiler given the code above?

I just don't see a DbProviderFactory or DbConnection anywhere in my project to exploit per the docs.

--

Tech tools and versions:

Target Framework: .NET Core 2.1 Output type: Console Application

OrmProfiler.exe = 2.0.1

SD Interceptor installed via NuGet: SD.Tools.OrmProfiler.Interceptor.NetCore = 2.0.0

--

Here is my Startup.cs class:

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using SportsStore.Models;

namespace SportsStore { public class Startup { public Startup(IConfiguration config) => Configuration = config;

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit [https://go.microsoft.com/fwlink/?LinkID=398940](https://go.microsoft.com/fwlink/?LinkID=398940)
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddTransient<IRepository, DataRepository>();
        services.AddTransient<ICategoryRepository, CategoryRepository>();
        services.AddTransient<IOrdersRepository, OrdersRepository>();

        string conString = Configuration["ConnectionStrings:DefaultConnection"];
        services.AddDbContext<DataContext>(options =>
                                           {
                                               options.EnableSensitiveDataLogging();
                                               options.UseSqlServer(conString, providerOptions => providerOptions.CommandTimeout(60));
                                           });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseDeveloperExceptionPage();
        app.UseStatusCodePages();
        app.UseStaticFiles();
        app.UseMvcWithDefaultRoute();
    }
}

}

--

The ORM Profiler docs state:

.NET Core 2.0+ At startup of your application, where a DbProviderFactory is used to initialize your data access technology, you have to use the InterceptorCore to wrap the DbProviderFactory with one provided by the interceptor so it can track the activity and how long each action takes.

using SD.Tools.OrmProfiler.Interceptor; //...

// example factory here is the SqlClient factory. You should provide the // factory you're using on .NET Core for your database type, e.g. // Oracle.ManagedDataAccess.Client.OracleClientFactory for Oracle var wrappedType = InterceptorCore.Initialize("application name", typeof(System.Data.SqlClient.SqlClientFactory)); // pass wrappedType now to your data access technology you're using instead of the // type passed to Initialize();

--

If your data access technology uses a DbConnection instance instead of a DbProviderFactory type, you can create one from the wrapped factory type by using:

var connection = wrappedType.Instance.CreateConnection(); // now pass 'connection' to your data access technology

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 15-Jul-2019 10:00:04   

You're using EF Core, not EF 6, as you're using .NET Core and EF Core is the EF versoin running on that, not EF 6.

I'm sorry but ORM Profiler doesn't support EF Core at the moment, as it doesn't use DbProviderFactory so we can't wrap the factory to intercept calls with EF Core. We can with EF 6 but not with EF Core.

Frans Bouma | Lead developer LLBLGen Pro
John Smith
User
Posts: 6
Joined: 24-Oct-2010
# Posted on: 15-Jul-2019 17:10:22   

Ok no worries. I justed to make sure it wasn't me. I'll just watch for future releases.