1

I'm using entity framework 6.02 and Vb.net with Sql server databases.

I have 4 identical databases on 4 different servers.I create Entity through wizard with one of the databases.Now I want to modify the connection string on runtime , to connect with other databases.

This is how I do this change :

This is the connection string on app.config inside visual studio ( I have changed server name with {SERVER} and database name with {DATABASE}

<connectionStrings>
 <add name="MyEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source={SERVER};initial catalog={DATABASE};integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
 </connectionStrings>

This is the code to change the connection string :

Public Cnstring as string

Dim connstringtemplate As String = System.Configuration.ConfigurationManager.ConnectionStrings(1).ConnectionString.ToString

Cnstring = connstringtemplate.Replace("{DATABASE}", "MyDB2").Replace("{SERVER}","PC2").Tostring

This is My Entity class to accept connection string as parameter :

Partial Public Class MyEntities
 Inherits DbContext   
Public Sub New(connectionString  As String)
     If String.IsNullOrWhiteSpace(connectionString) Then
          Throw New ArgumentNullException("connectionString")
     End If
     Database.Connection.ConnectionString = connectionString
 End  Sub

This is Entity declaration :

context = New MyEntities(cnstring)

But now on runtime i get an error on the line

Database.Connection.ConnectionString = connectionString
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Keyword not supported: 'metadata'.

What's wrong with this code ?

Thank you !

3
  • Where did you get that connection string from? Commented Mar 19, 2014 at 12:18
  • This connection string is inside app.config. Commented Mar 19, 2014 at 12:28
  • Yes is auto-generated Commented Mar 19, 2014 at 12:40

1 Answer 1

1

The DbContext constructor accepts a connection string as a parameter. You may be better off building a connection string and passing that to the constructor.

I have used something like:

// the model name in the app.config connection string (any model name - Model1?)
private static string GetConnectionString(string model, settings)
{
    // Build the provider connection string with configurable settings
    var providerSB = new SqlConnectionStringBuilder
    {
        InitialCatalog = settings.InitialCatalog,
        DataSource = settings.DataSource,
        UserID = settings.User,
        Password = settings.Password
    };

    var efConnection = new EntityConnectionStringBuilder();
    // or the config file based connection without provider connection string
    // var efConnection = new EntityConnectionStringBuilder(@"metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=System.Data.SqlClient;");
    efConnection.Provider = "System.Data.SqlClient";
    efConnection.ProviderConnectionString = providerSB.ConnectionString;
    // based on whether you choose to supply the app.config connection string to the constructor
    efConnection.Metadata = string.Format("res://*/Model.{0}.csdl|res://*/Model.{0}.ssdl|res://*/Model.{0}.msl", model); ;
    return efConnection.ToString();

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.