Search code examples
c#entity-frameworkextension-methodsdbcontextasp.net-core-2.0

How to use a dbcontext in a static class? (ObjectDisposedException)


Hey I just recently learnt how to use extension methods and pretty excited to implement it in my current project.

My objective: I want to check whether an entry exists in my table in a helper class, since I'm going to use it in multiple controllers to be able to determine which navigation links to show in my navbar:enter image description here

My Issue: I don't know how to access my dbcontext in my static helper class. My dbcontext controller takes an argument I don't know how to pass in my static class. I think creating a new dbcontext would solve my scope issue explained below but I don't see how I can pass the optional argument to my constructor.

enter image description here

It is currently configured in the Startup.cs class. enter image description here

What I tried: Passing the ApplicationDbContext as an argument. This works for a single method call in my controller, but when calling multiple extension methods (To check which game accounts the user has) I get a ObjectDisposedException.

ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ApplicationDbContext'.

From what I understand it is a scope issue where the first method call disposes the context when it's done and I'm trying to use the same context in the next call? What can I do to work around that?

I tried reading this link Cannot access a disposed of object in ASP.NET Core when injecting DbContext but it didn't help me since it requires the ApplicationBuilder which is in the Startup.cs class.

enter image description here

Solution Update I disposed the dbcontext after every method call because I put it into a variable. Instead, I call it directly on the passed context and it works:

enter image description here


Solution

  • Yeah, so, although the extensions are new and shiny to you, that doesn't mean you should use them for everything. First, extensions should have a logical connection to the type they're operating on. For example, if you have a string, something like ToUpper() makes sense as an extension because it modifies and returns a string. Something like what you're trying to do: just using the value of the reference to return a completely foreign type is a violation of the extension pattern.

    Second, an extension should not interact with something like a database. In particular here, the static nature of an extension is completely incompatible with the concept of a EF context object. The only way you could even get it to work is to actually new up a context each time the extension is called, inside the extension. That's both a great way to screw up the EF object tracking stuff and a great way to leak memory.

    Long and short, don't do this.

    If you're just trying to factor out this code, you have better options. For example, you can actually just add methods directly to your context.

    public class ApplicationDbContext : DbContext
    {
        ...
    
        public bool HasDota2Account(string id)
        {
            return Dota2Accounts.Any(m => m.ApplicationUserId == id);
        }
    }
    

    Then, in your controller, you can simply do:

    var hasDota2Account = context.HasDota2Account(User.Identity.GetUserId());