title: DbContext error when using it for ActionFilter in ASP.NET Core MVC
body: <p>I need to log certain user actions, such as Login, logout, CRUD, etc. and save to a table called <code>AuditRecords</code>. I have a <code>DbContext</code> called <code>AuditContext</code>:</p>
  <p>I have learned that applying an <code>ActionFilter</code> and decorate a controllermethod with it is the way to go. The <code>AuditContext</code> is causing a red squiggly asking for the 'options', which baffles me. I have tried DI, but got the same results. Any thoughts? I am on DotNet 5.01, but I tried it withg DotNet 6 as well with the same results.</p>
  <p><strong>EDIT</strong>
  I changed the AuditAttribute and Context to make it as straightforward as possible and by using Dependency Injection, as advised. But the problem is still not solved.</p>
  <pre><code> public class Audit
  {
      //Audit Properties
      public Guid AuditID { get; set; }
      public string UserName { get; set; }
      public string IPAddress { get; set; }
      public string AreaAccessed { get; set; }
      public DateTime Timestamp { get; set; }
  
      //Default Constructor
      public Audit() { }
  }
  
  public class AuditContext : DbContext
  {
      public DbSet&lt;Audit&gt; AuditRecords { get; set; }
  }
  public class HomeController : Controller
  {
     //red squiggly under [Audit] decoration with error of no formal
     //parameter 'context' in AuditAttribute.AuditAttribute(AuditContext)
      [Audit]  
      public IActionResult Privacy()
      {
          return View();
      }
  
  //AuditAttribute class
  using Microsoft.AspNetCore.Mvc.Filters;
  using System;
  
  namespace LoggingDemoMVC.Filters
  {
  public class AuditAttribute : ActionFilterAttribute
  {
      private readonly AuditContext _context;
  
      public AuditAttribute(AuditContext context)
      { 
          _context = context;
      } 
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          //Stores the Request in an Accessible object
          var request = filterContext.HttpContext.Request;
          //Generate an audit
          Audit audit = new Audit()
          {
              //Your Audit Identifier     
              AuditID = Guid.NewGuid(),
              //Our Username (if available)
              UserName = (filterContext.HttpContext.User.Identity.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : &quot;Anonymous&quot;,
              //The IP Address of the Request
              IPAddress = request.Path,
              //The URL that was accessed
              AreaAccessed = request.HttpContext.Request.Path,
              //Creates our Timestamp
              Timestamp = DateTime.UtcNow
          };
  
          //Stores the Audit in the Database
          
          _context.AuditRecords.Add(audit);
          _context.SaveChanges();
  
          //Finishes executing the Action as normal 
  
          base.OnActionExecuting(filterContext);
      }
  }
  </code></pre>
  <p>}</p>