Solved

Setting connection string for Entity Framework at runtime vb.net

Posted on 2016-10-01
5
46 Views
Last Modified: 2016-11-09
Hi,

I have a class made with Entity Framework designer.

I have a test and production database that's identical.

I want to switch between test and production at runtime by changing the connection string.

How can I do this in VB.net ?
0
Comment
Question by:debonod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41824984
When you create an instance of your context, you can pass your connection string but it is not a regular connection string. I use this method in my application to build my EF connection string:
  Public Shared Function EFBDConnectionString(ByVal pMetaData As String, ByVal pDatabaseName As String) As String
        'Specify the provider name, server and database.
        Const kStrProviderName As String = "System.Data.SqlClient"
        'Const strDatabaseName As String = "FGLMainData"

        Dim strServerName As String = GetValueFromConnectionString("Data Source")

        'Initialize the connection string builder for the underlying provider.
        Dim sqlBuilder As New SqlConnectionStringBuilder()

        'Set the properties for the data source.
        sqlBuilder.DataSource = strServerName
        sqlBuilder.InitialCatalog = pDatabaseName
        sqlBuilder.ApplicationName = GetValueFromConnectionString("Application Name") + " (EF)"

        If String.IsNullOrEmpty(GetValueFromConnectionString("Integrated Security")) Then
            sqlBuilder.UserID = GetValueFromConnectionString("UID")
            sqlBuilder.Password = GetValueFromConnectionString("PWD")
        Else
            sqlBuilder.IntegratedSecurity = True
        End If

        'Build the SqlConnection connection string.
        Dim strProviderString = sqlBuilder.ToString() + ";MultipleActiveResultSets=true;"

        'Initialize the EntityConnectionStringBuilder.
        Dim entityBuilder = New EntityConnectionStringBuilder()
        
        'Set the provider name.
        entityBuilder.Provider = kStrProviderName

        'Set the provider-specific connection string.
        entityBuilder.ProviderConnectionString = strProviderString

        'Set the Metadata location.
        entityBuilder.Metadata = String.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", pMetaData)

        Return entityBuilder.ToString()
    End Function

Open in new window

0
 

Author Comment

by:debonod
ID: 41825151
Hi Eric,

The trouble is that I'm using the class generated by the EF Designer.

There is no way I can see to use the connection string as a parameter to that class.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41825344
it is in your app.config file
0
 

Author Comment

by:debonod
ID: 41825518
Yes, I know.

But it doesnt help much on runtime.

Anyways I found a solution myself about this.
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question