0

Im having this problem when changing my entities connection string.

here is the code:

SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
                        sqlBuilder.DataSource = @"KURT-PC\SQLEXPRESS";
                        sqlBuilder.InitialCatalog = "KurtDB";
                        sqlBuilder.UserID = "Admin";
                        sqlBuilder.Password = "123456";
                        sqlBuilder.IntegratedSecurity = false;
                        sqlBuilder.MultipleActiveResultSets = true;

                        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
                        entityBuilder.Provider = "System.Data.SqlClient";
                        entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
                        entityBuilder.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";

                        this.Entities.Connection.ConnectionString = conn.Connection.ConnectionString = entityBuilder.ConnectionString.ToString();

when it tries to change the connection string its giving me an InvalidOperationEsception.

error details: No modifications to connection are permitted after the metadata has been registered either by opening a connection or constructing the connection with a MetadataWorkspace.

Any help its leaving me speachless....

1 Answer 1

4

I'm assuming that in your example, this.Entities is an instance of an Entity Framework Object Context.

The error message means that you cannot simply assign the new connection string to the Connection property of the Entities object.

Instead, try passing it as a constructor parameter:

  • locate the line where you instantiate Entities:
    this.Entities = new ...();
  • change it to something like this:
    this.Entities = new ...(entityBuilder.ConnectionString.ToString());
Sign up to request clarification or add additional context in comments.

2 Comments

i tried but it still keeps the same connection string which it was assigned with... KurtDBEntities conn = new KurtDBEntities(sqlBuilder.ConnectionString.ToString()); then it goes through this constructor: public KurtDBEntities(string connectionString) : base(connectionString, "KurtDBEntities") { this.ContextOptions.LazyLoadingEnabled = true; OnContextCreated(); } And im having an argumentException with the details:
Bdw new to stackoverflow so dont really know how to make proper formattin sorry...

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.