Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting connection string for Entity Framework at runtime vb.net

Posted on 2016-10-01
5
Medium Priority
?
61 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 2000 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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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