PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] How to connect to different Db instance at run time-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] How to connect to different Db instance at run time

  1. #1

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Resolved [RESOLVED] How to connect to different Db instance at run time

    I apologize in advance. I am sure this has been answered here at some point but I can't find search terms that don't result in many unrelated posts.

    I created an app with EF. Now I want to install it on a new machine with a difference Db instance name. I have user settings they can change. But what is the best practice? I'm going to guess and please tell me if there is a better way.

    I'm guessing each time one instantiates the context one should change the property objContext.Database.Connection.ConnectionString using the user settings. Is that correct?

  2. #2

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: How to connect to different Db instance at run time

    I see what I wanted to do is impossible. I'm reading that one needs to create a partial class in a non-generated file to create this in the constructor. But all the examples I see online are C#, does anyone have an example of this I can learn from?

  3. #3

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: How to connect to different Db instance at run time

    I got it to work. I found Jim's post and made a class just like it. I had a class already that would do SQL maintenance and it had a common SQLConnection object (ConnMain) already which was created using My.Settings. I then added this function to my DbUtilities class:
    vb.net Code:
    1. Public Function MakeConnectionString() As String
    2.     Dim objEFBuilder As New Entity.Core.EntityClient.EntityConnectionStringBuilder()
    3.     objEFBuilder.Metadata = "res://*/Db.KeeperModel.csdl|res://*/Db.KeeperModel.ssdl|res://*/Db.KeeperModel.msl"
    4.     objEFBuilder.Provider = "System.Data.SqlClient"
    5.     objEFBuilder.ProviderConnectionString = connMain.ConnectionString
    6.     Return objEFBuilder.ConnectionString
    7. End Function
    Now when I instantiate the Context, I have an overload that accepts a connection string. I then call this function and it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width