Manually open and close connection with entity framework

connection entity-framework explicit

Question

I have been searching the web since yesterday but cannot find an example which may solve my issue.

I am trying open and close the connection manually with the below code

        Using conn = New EntityConnection(entityBuilder.ToString)

            Using ctx As New EbosEntities()

                 conn.Open()

The problem is, how do i write a constructor(Entity Class) which will take the conn as parameter? otherwise this connection will have nothing to do with the (new context) declaration, I guess. Like below,

           ctx As New EbosEntities(conn)

If I want to use default connection string can I just write

           ctx.Database.Connection.Open()

Many Thanks.

1
0
9/21/2013 9:00:56 AM

Popular Answer

I don't know how to do it on VB, but with C# you can try to do something like this:

    var datacontext = new EbosEntities();
    try
    {                
        datacontext.Connection.Open();
        //do some work

    }
    finally
    {
        datacontext.Connection.Close();
    }
2
9/21/2013 9:08:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow