Why does no one disposes DbContext after WebApi controller operation?

后端 未结 6 2544
梦谈多话
梦谈多话 2021-02-19 20:32

I am aware of various tutorials as well as complete examples targeting WebApi & Entity Framework (even from Microsoft) that have WebApi

相关标签:
6条回答
  • 2021-02-19 20:42

    You are supposed to Dispose() of your context class, so use the using construct:

    using (var context = new InternetDbContext())
    {    
        // your code here, try/catch is auto-generated by the compiler
    }
    
    0 讨论(0)
  • 2021-02-19 20:43

    Personally, whenever I see the type implements IDisposable, I'm almost certain that I'm going to use a using statement when working with new instances of this type.

    When the variable goes out of scope (like in your case with the context variable going out of scope when the execution returns from GetInternet method), its memory is eventually going to be reclaimed by garbage collector but this doesn't mean that any native handlers (e.g. file handlers or database connections) are going to be closed which can have a very serious negative impact on your application.

    So, consider always wrapping an IDisposable into the using construct:

    using (var context = new InternetDbContext())
    {
      // Your code goes here
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-19 20:43

    Sometimes it is a bad idea to dispose the context. For example, I have a WebAPI2 controller method like this,

        [Route("Questionnaires")]
        public IEnumerable<Questionnaire> GetAllQuestionnaires()
        {
            NMQContext context = new NMQContext();
            return context.Questionnaires.AsEnumerable();
        }
    

    The result data is a JSON list, and the Questionnaire is a compound object -- it contains entities from multiple database tables. If I wrap this with "using" I get an error, such as

       "Message": "An error has occurred.",
       "ExceptionMessage": "The operation cannot be completed because the DbContext has been disposed.",
    

    If you are trying to serialize compound objects, then it is better NOT to dispose the connection. It is better to let the EF handle it for you. You could probably fix it by explicit eager loading, but that is painful. Don't do it.

    0 讨论(0)
  • 2021-02-19 20:50

    It's because they're wrong. Don't trust other people's code, especially when it's part of an online tutorial.

    0 讨论(0)
  • 2021-02-19 20:56

    You should call Dispose() or use using when working with DbContext but you don't have to.

    If you want to play safe, always wrap in a using or Dispose() but if you want to have a better understanding of EF DbContext implementation, keep reading.

    Long story short, EF usually knows when is the time to Close a connection, so in the majority of the cases, call or not call Dispose() does the same result and doesn't have any impact in memory usage or performance because garbage collector will handle properly, unlike most of the IDisposable classes.

    But, there are two main reasons that you should wrap in using or call Dispose() specifically for EF DbContext.

    First is when someone manually open a connection with ObjectContext from the DbContext, if you don't call Dispose()/using you could leave open connections as those connections will not be managed automatically by EF.

    The second reason is that the derived class of DbContextthat you are instantiating could be overriting the default behavior of dispose for example to aggregate the disposal of other unmanaged resources into the lifetime of the context, so if not properly disposed, it'll leave those resources alive.

    This article is a must read.

    0 讨论(0)
  • 2021-02-19 20:57

    Prime case for using, no matter how the method is exited, your DbContext will be disposed;

    public HttpResponseMessage GetInternet(int id) {
        using(var context = new InternetDbContext()) {
            var result =
               (from internet in context.Internets
                where internet.Id.Equals(id)
                select internet).FirstOrDefault();
            if(result != null)
               Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
    
    0 讨论(0)
提交回复
热议问题