title: Cannot access a disposed context instance EF core
body: <p>I am developing a CRM by .net core mvc and EF which needs a lot of DB connection to retrieve and update information. From time to time I face this error during debugging, this is a big project with a lot of users I don't know how it will work in real usage!</p>
  <blockquote>
  <p>Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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 instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.\r\nObject name: 'XXX'.</p>
  </blockquote>
  <p>before I used this setting in startup.cs:</p>
  <pre><code>services.AddDbContext&lt;XXX&gt;
                  (option =&gt; 
                  option.UseSqlServer(Configuration.GetConnectionString(&quot;YYY&quot;))
                  .ServiceLifetime.Singleton);
  </code></pre>
  <p>that setting was lead to another error occasionally:</p>
  <blockquote>
  <p>System.InvalidOperationException: The instance of entity type 'TblZZZ' cannot be tracked because another instance with the same key value for {'ZZZId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.</p>
  </blockquote>
  <p>so I have changed my setting to:</p>
  <pre><code>services.AddDbContext&lt;XXX&gt;
                  (option =&gt; 
                  option.UseSqlServer(Configuration.GetConnectionString(&quot;YYY&quot;))
                  .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
  </code></pre>
  <p>now I am facing the <code>Cannot access a disposed context instance</code>. I don't know what setting can solve my issue.</p>
  <h1>I m using net5.0</h1>
  <p>Update:
  my partial class:</p>
  <pre><code>public partial class TblXXX
      {
          private ZZZ context;//context
          public TblXXX(ZZZ _context)
          {
              context = _context;
          }
          public TblXXX() { }
  //function for check username
     public bool CheckUsernameExit(string username) {
              var u = context.TblXXX
                  .Where(e =&gt; e.Username == username)
                  .FirstOrDefault();
              return (u != null);
          }
  }
  </code></pre>
  <p>in my controller:</p>
  <pre><code>  public IActionResult Check(UserModelView user)
          {
              if (ModelState.IsValid)
              {
                  var r = _viewModel.tblXXX.CheckUsernameExit(user.tblXXX.Username);
                  if (r)
                  {
                      toastNotification.AddErrorToastMessage(&quot;This email is in use. Enter differernt email.&quot;);
                   
                      return View(&quot;Create&quot;, _viewModel);
                  }
              
  </code></pre>
  <p>and this is my ViewModel:</p>
  <pre><code> public class UserModelView
      {
          public TblXXX tblXXX { get; set; }
          public List&lt;TblXXX &gt; tblXXXList { get; set; }
      }
            
  </code></pre>
  <p>this is one of the cases I face the error.</p>