Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Multiple DbContexts - only logging one of them #114

Open
dazinator opened this issue Feb 24, 2016 · 3 comments
Open

Multiple DbContexts - only logging one of them #114

dazinator opened this issue Feb 24, 2016 · 3 comments

Comments

@dazinator
Copy link

In My application, I have 2 DbContexts, defined in seperate projects.

I have noticed that when accessing a page that utilises both of these dbcontexts to perform some query, only one of the dbcontexts is being captured by Glimpse:

image

(CompanyMenu VC is atually performing a long running EF query with another DbContext there, but glimpse isn't capturing it)

How does Glimpse "discover" the DbContexts and how can "inform" glimpse of this other one that is being used?

@dazinator
Copy link
Author

I have figured out the cause of this issue.

It's to do with the way the other DbContext is registered and resolved.

If you register it like so:


  string connectionString = Configuration[ConfigKeys.ConnectionString];
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<SomeDbContext>(options => options.UseSqlServer(connectionString));

and then inject SomeDbContext into things, then Glimpse picks it up and it works. However if you define an interface for your context, and resolve it against that interface instead, then Glimpse doesn't pick it up - i.e:

  string connectionString = Configuration[ConfigKeys.ConnectionString];
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<SomeDbContext>(options => options.UseSqlServer(connectionString));

 services.AddScoped<ISomeContext, SomeDbContext>();

And then inject ISomeContext - it doesn't work.

However changing that to:

  string connectionString = Configuration[ConfigKeys.ConnectionString];
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<SomeDbContext>(options => options.UseSqlServer(connectionString));

 services.AddScoped<ISomeDbContext>((provider) =>
            {
                var dbContext = provider.GetService<SomeDbContext>();
                return dbContext;
            });

Fixes it again, allowing ISomeDbContext to be injected.

Closing for now as assuming there isn't anything that can be done about this.

@avanderhoorn
Copy link
Member

@anpete any idea why we might be missing events in this use case?

@dazinator
Copy link
Author

Im not sure if this makes a difference but I am also using autofac as the underlying ioc container, rather than the default one provided by microsoft. I suspect you can replicate this in either scenario but i havent checked.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants