VB.NET Entity Framework Using multiple databases

Joseph Mancini 41 Reputation points
2023-06-09T15:41:14.4233333+00:00

I'm trying to setup Entity Framework 6 in my VB.Net desktop application. I can get it working fine using Production database, but, this is a program where you can login to either production or the test database. When I created the model, I set it up to use production. I would like to be able to use the test database when I login for when I need to do some testing.

I have found various examples, but nothing seems to work. I tried the example where it is supposed to modify the production connection string in app.config so that it uses the test database instead, but that ends up modifying the connection string in the app.exe.config file, and my program barks when I do that anyways.

I found another example to set the connection string to the TEST database within the using db context block as below, but again, that ends up barking also. Am I on the right path, but missing something, or is there a better way to do what I would like to do?

            Dim UserEntry As User
            Using db = New UserEntities
                db.Database.Connection.ConnectionString = myConnString
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,369 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Joseph Mancini 41 Reputation points
    2023-06-12T19:52:10.2833333+00:00

    Jack - I found the link below that changes the database at runtime by passing the connection string, with either the Live or Test database name in the string. I've implemented it and it seems to work ok so far.

    As far as your suggestion - I had thought about doing that, but I didn't think it was efficient to check which database to connect to each time I access the dbcontext.

    https://www.codeproject.com/Tips/798392/Changing-Databases-at-Run-time-using-Entity-Framew