Solved

Setting connection string for Entity Framework at runtime vb.net

Posted on 2016-10-01
5
20 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
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 69

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

18 Experts available now in Live!

Get 1:1 Help Now