locked
Set up entity framework connection string outside of app.config file RRS feed

  • Question

  • Hi all,

    Can someone please give me an example of how to set up an entity framework connection string through code rather than having it in the app.config file?

    What I have is a standalone windows forms application that uses TCP to access a remote database using entity framework & need to have the option to change the default database that a particular machine would use, and also allow the default DB to be changed without having to send out a full update to every user. Instead, I could just send a new settings file that would contain the location of the new database, or just the new database location to paste into program settings perhaps, but not the full connection string so the database password can remain hard-coded.

    I've found a couple of forum responses that just say to connect directly, but my attempts to figure out how to do this have failed! 

    Any help would be appreciated.

    Tuesday, August 9, 2016 10:44 PM

Answers

  • Thank you all for your replies.

    After a bit more searching, it was as simple as making this change in the Entity Data Model / context file:

        Public Sub New()
            MyBase.New("name=mycontext") <<This references the app.config file by default and needs to be removed
            MyBase.Database.Connection.ConnectionString = "Data Source=" & myPublicDatabaseLocation & ";Initial Catalog=myInitialCatalog;User ID=sa;Password=1234567"

        End Sub

    And then MyPublicDatabaseLocation is a public variable that can be set from anywhere.

    Saturday, August 13, 2016 10:37 PM

All replies

  • Tuesday, August 9, 2016 11:35 PM
  • Have not actually tried this but worth a shot. If using code first, create an overloaded constructor and pass in pass in the connection string while model first you can't because the DbContext is auto-generated.

    Code would look something like this for code first.

    Partial Public Class YourEntities
        Inherits DbContext
        Public Sub New()
            MyBase.New("name=YourEntities")
        End Sub
        Public Sub New(ByVal ConnectionString As String)
            MyBase.New(ConnectionString)
        End Sub
    End Class

    Then have the connection string setup still in app.config

    <connectionStrings>   
    <add name="YourEntities" connectionString="..." providerName="System.Data.EntityClient" />
    </connectionStrings>

    Check out the following, again have not tried it and you may have seen it already but thought it prudent to show it

    https://msdn.microsoft.com/en-us/data/jj592674.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, August 9, 2016 11:41 PM
  • Thank you all for your replies.

    After a bit more searching, it was as simple as making this change in the Entity Data Model / context file:

        Public Sub New()
            MyBase.New("name=mycontext") <<This references the app.config file by default and needs to be removed
            MyBase.Database.Connection.ConnectionString = "Data Source=" & myPublicDatabaseLocation & ";Initial Catalog=myInitialCatalog;User ID=sa;Password=1234567"

        End Sub

    And then MyPublicDatabaseLocation is a public variable that can be set from anywhere.

    Saturday, August 13, 2016 10:37 PM
  • Hi Derek Bollocks,

    I am glad to know you solved this problem and thanks for sharing the solution. 

    It will be very beneficial for other community members who have the similar questions. 


    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 15, 2016 6:04 AM