Solved

Setting connection string for Entity Framework at runtime vb.net

Posted on 2016-10-01
5
15 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
  • 2
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
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
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
it is in your app.config file
0
 

Author Comment

by:debonod
Comment Utility
Yes, I know.

But it doesnt help much on runtime.

Anyways I found a solution myself about this.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Launch Process with NOT as Administrator 15 65
Vb.Net Menu 1 21
String manipulation 15 49
Achieve json result 2 28
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now