Remove model parameterless constructor

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
ajmoore79
Posts: 5
Joined: Fri 26 Jul 2013 13:08

Remove model parameterless constructor

Post by ajmoore79 » 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..

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

ajmoore79
Posts: 5
Joined: Fri 26 Jul 2013 13:08

Re: Remove model parameterless constructor

Post by ajmoore79 » Fri 13 Jun 2014 12:19

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

Shalex
Site Admin
Posts: 8295
Joined: Thu 14 Aug 2008 12:44

Re: Remove model parameterless constructor

Post by Shalex » Tue 17 Jun 2014 13:27

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.

Post Reply