61885

Error while Changing the connection string dynamically

Question:

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....

Answer1:

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:

<ul><li>locate the line where you instantiate Entities:<br />this.Entities = new ...();</li> <li>change it to something like this:<br />this.Entities = new ...(entityBuilder.ConnectionString.ToString());</li> </ul>

Recommend

  • simple way to include node_modules in laravel 5.5
  • How to communicate with .NET 3.5 SP1 bootstrapper
  • Perforce Integration: Not able to submit the changes to streams from classic work space
  • Is is possible to integrate Joomla and Codeigniter? [closed]
  • How does one include sparklines and other per-row graphics in a Google Table (or similar)?
  • Implementing a friends relationship in Symfony3 with Doctrine
  • When will it become active on admob at Android app
  • Reading Element attribute value using XStream
  • Where i upload APNS/p12 cert file in Firebase Console?
  • is it possible to run a task scheduler in bottle web framework
  • gulp command not recognized in Jenkins
  • Get users zipcode using facebook javascript api
  • Upload to my Google Drive with php
  • is paypal's iphone sdk for payment approved by apple? [closed]
  • D3.js: Define ordinal axis from JSON data
  • web shop (shopping cart) on google app engine
  • OleDbDataAdapter not filling all of the rows
  • Can't connect Entity Framework to local SQL Server Express
  • Using SWIG with a build system [closed]
  • Dynamically set LESS variables from user settings
  • Relative paths. baseUrl and paths not working on ionic2 - angular2
  • PHP multiple file uploads
  • PHP CURL timing out but CLI CURL works
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Validaiting emails with Net.Mail MailAddress
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • trying to dynamically update Highchart column chart but series undefined
  • embed rChart in Markdown
  • How to stop GridView from loading again when I press back button?
  • How does Linux kernel interrupt the application?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app