Remove model parameterless constructor
Posted: Thu 12 Jun 2014 15:19
Alright, I have a quick question. I am using Visual Studio 2013-vb.net, Latest Devart Entity Developer version, SQL Server 2008. May not have anything to do with my questions, but I'll mention that I have a basic model built in entity developer- nothing special except I do have it setup "Generate Partial Class=true".
My question-
When my application starts, I needed to be able to prompt the user to select either a production or test database to use. So I figured out how to copy my database in SQL server and change the connection string on startup. It seems to works well so far. Next I need to change my code where I declare a new object context to pass along either the test or product connection string instead of just using the parameterless constructor of the model. For instance, change "Using db as new dbContext" to "using db as new dbContext(connectionStringHere)". Simple enough to go through the code and change them all, but I want to make sure that all future code will be forced to pass the connection string. So I thought I would just comment out the parameterless "New" constructor on the model so that it cant be used... See any issues with this?
By the way, here is my quick code to switch connections if anyone needs it... basically, when the program starts, "SetCurrentConnectionString(Live/Test)" will set a variable called "CurrentConnectionString" to either a live or test connectionstring. Then when you make an instance of the objectContext throughout your program, just pass along the variable "CurrentConnectionString". I'm sure they have many ways to do this...I would put something like this directly in the model constructor, but I'm not sure how that would cause any problems either..
Thanks in advance-
Aaron
My question-
When my application starts, I needed to be able to prompt the user to select either a production or test database to use. So I figured out how to copy my database in SQL server and change the connection string on startup. It seems to works well so far. Next I need to change my code where I declare a new object context to pass along either the test or product connection string instead of just using the parameterless constructor of the model. For instance, change "Using db as new dbContext" to "using db as new dbContext(connectionStringHere)". Simple enough to go through the code and change them all, but I want to make sure that all future code will be forced to pass the connection string. So I thought I would just comment out the parameterless "New" constructor on the model so that it cant be used... See any issues with this?
By the way, here is my quick code to switch connections if anyone needs it... basically, when the program starts, "SetCurrentConnectionString(Live/Test)" will set a variable called "CurrentConnectionString" to either a live or test connectionstring. Then when you make an instance of the objectContext throughout your program, just pass along the variable "CurrentConnectionString". I'm sure they have many ways to do this...I would put something like this directly in the model constructor, but I'm not sure how that would cause any problems either..
Code: Select all
Public Sub FormLoad()
SetCurrentConnectionString(dbConnectionTypeCode.Test) 'Specify either Live or Test
Using db As New dbModel.dbEntities(CurrentConnectionString) 'Create an instance passing the connection string
Dim q = (From s In db.orders).Count 'Just a test query
MsgBox(q)
End Using
end sub
Public CurrentConnectionString As String
Public Enum dbConnectionTypeCode
Live = 0
Test = 1
End Enum
Public Sub SetCurrentConnectionString(dbTypeToSet As dbConnectionTypeCode)
Select Case dbTypeToSet
Case Is = dbConnectionTypeCode.Live
'LIVE DB
Dim sqlBuild As New SqlClient.SqlConnectionStringBuilder
With sqlBuild
.DataSource = "sqlLiveServerName"
.InitialCatalog = "dbLiveDatabaseName"
.IntegratedSecurity = False
.PersistSecurityInfo = True
.UserID = "UserName"
.Password = "Password"
End With
Dim ecb As New EntityConnectionStringBuilder With {.Provider = "System.Data.SqlClient",
.ProviderConnectionString = sqlBuild.ToString(),
.Metadata = "Get This from your existing connection string in app.config"}
Console.WriteLine(ecb.ToString) 'just a test
CurrentConnectionString = ecb.ToString
Case Is = dbConnectionTypeCode.Test
'TEST DB
Dim sqlBuild As New SqlClient.SqlConnectionStringBuilder
With sqlBuild
.DataSource = "sqlTestServerName"
.InitialCatalog = "dbTestDatabaseName"
.IntegratedSecurity = False
.PersistSecurityInfo = True
.UserID = "UserName"
.Password = "Password"
End With
Dim ecb As New EntityConnectionStringBuilder With {.Provider = "System.Data.SqlClient",
.ProviderConnectionString = sqlBuild.ToString(),
.Metadata = "Get This from your existing connection string in app.config"}
Console.WriteLine(ecb.ToString) 'just a test
CurrentConnectionString = ecb.ToString
End Select
End Sub
Aaron