none
kepp connection open with entity framework 5?

    Question

  • Hello,

    Using Entity framework 5 there is no constructor function that used in version 4 for setting a connection string in order to keep it open and close it manually like for example

    using(var context = new MyContext("name=MyConStr")
    {
       // do stuff
    }

    I know I can modify the generate Entity class constructor function but I would like to avoid that.

    Question is what is the way to manually control connection open/close with Entity framework 5? 

    Thanks,
    Asaf

    Wednesday, July 24, 2013 4:53 PM

Answers

  • Hi asafgo;

    A couple of things, first in EF 5 as you stated does not work like that any more, In EF 5 the way to open the connection is to use the following.

    // To open the connection
    context.Database.Connection.Open();
    // To close the connection
    context.Database.Connection.Close();
    // To Check the status of the connection.
    context.Database.Connection.State

    I read that they were having issues with this but it should be fixed by EF 5. Well from my testing the minute you execute a query it closes the connection.

    The following will work from the testing I just did, so you may want to try this. The context in the commands below is the DbContext instance name.

    // To open the connection
    ((IObjectContextAdapter)context).ObjectContext.Connection.Open();
    // To close the connection
    ((IObjectContextAdapter)context).ObjectContext.Connection.Close();
    // To Check the status of the connection.
    ((IObjectContextAdapter)context).ObjectContext.Connection.State
    Where State returns an Enumeration of ConnectionState such as ConnectionState.Open

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by asafgo Wednesday, July 24, 2013 8:08 PM
    Wednesday, July 24, 2013 8:03 PM

All replies

  • Hi asafgo;

    Please see this Microsoft web site, Managing Connections and Transactions, for the answer to your question.

      

     

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, July 24, 2013 5:15 PM
  • Hi Fernando,

    From the website you have pointed me there is an example

    // Create an EntityConnection.
    EntityConnection conn = new EntityConnection("name=AdventureWorksEntities");
    
    // Create a long-running context with the connection.
    AdventureWorksEntities context = new AdventureWorksEntities(conn);
     

    As I have written in my question Entity Framework 5 does not generate a construction function that takes a connection string or connection object like in Entity Framework 4.

    So my question remain the same of how to manually manage connection with Entity Framework 5.

    Asaf 

    Wednesday, July 24, 2013 6:32 PM
  • Hi asafgo;

    A couple of things, first in EF 5 as you stated does not work like that any more, In EF 5 the way to open the connection is to use the following.

    // To open the connection
    context.Database.Connection.Open();
    // To close the connection
    context.Database.Connection.Close();
    // To Check the status of the connection.
    context.Database.Connection.State

    I read that they were having issues with this but it should be fixed by EF 5. Well from my testing the minute you execute a query it closes the connection.

    The following will work from the testing I just did, so you may want to try this. The context in the commands below is the DbContext instance name.

    // To open the connection
    ((IObjectContextAdapter)context).ObjectContext.Connection.Open();
    // To close the connection
    ((IObjectContextAdapter)context).ObjectContext.Connection.Close();
    // To Check the status of the connection.
    ((IObjectContextAdapter)context).ObjectContext.Connection.State
    Where State returns an Enumeration of ConnectionState such as ConnectionState.Open

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by asafgo Wednesday, July 24, 2013 8:08 PM
    Wednesday, July 24, 2013 8:03 PM
  • Hello Fernando,

    your second solution with IObjectContextAdapter is working, thanks for your help.

    Asaf

    Wednesday, July 24, 2013 8:10 PM
  • Not a problem, glad to help.

    From my reading that issues should be taken care of in EF 6, for the first way.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, July 24, 2013 8:30 PM
  • Hi asafgo,

    I'm glad to hear that you got it working.Thanks for Fernando's answer at the same time.

    have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, July 27, 2013 3:10 AM