EF Framework , Repository ,Dependency Injection RRS feed

  • Question

  • Hello

    We are using Entity Framework 4.3.1 code first. We are using repository pattern and AutoFac as IoC container. DbContext is being injected into the repositories using AutoFac. With this we can control lifetime of DbContext instances using AutoFac. Are there any issues with this implementation?

    What are the best practices around the life time for DbContext instances, should those be short lived (per call), or those can live till worker process is up. Any help on this would help. I was reading creating a model ( a new DbContext instance ) is an expensive affair.


    Prasad Pimparkar


    Monday, October 8, 2012 6:37 PM


  • Create a new instance of DbContext everytime you do a unit update is the best practice to control the lifetime of DbContext. Don't let it live longer, otherwise, some unpredictable exception may be thrown out.

    Go go Doraemon!

    • Marked as answer by Allen_MSDN Monday, October 15, 2012 2:06 AM
    Wednesday, October 10, 2012 1:47 AM