Page 1 of 1

Remove model parameterless constructor

Posted: Thu 12 Jun 2014 15:19
by ajmoore79
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..

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


Thanks in advance-
Aaron

Re: Remove model parameterless constructor

Posted: Fri 13 Jun 2014 12:19
by ajmoore79
Any thoughts on this? I found that if I comment out the New() constructor and regenerate my model, it will uncomment/overwrite it. Can I make the template somehow not generate a parameterless "sub New()" for my object context?
Aaron

Re: Remove model parameterless constructor

Posted: Tue 17 Jun 2014 13:27
by Shalex
1. If you use parameterless constructor, the connection string (named like context descendant name) will be retrieved from *.config of your application. Just modify *.config of your deployed application to change between Live and Test connection strings.
2. If you want to use constructor with parameter, why are you removing parameterless constructor from code generation? If there is a reason, please modify a predefined template.