title: Attempted to read or write protected memory in Entity Framework
body: <p>I'm trying to dynamically change connection string for Entity framework (v4.4) context, I get this error : </p>
  
  <blockquote>
    <p>System.AccessViolationException was unhandled</p>
    
    <p>Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p>
  </blockquote>
  
  <p>The code that causes the error looks something like the following (I changed the names). </p>
  
  <pre><code>private Project1.Data.Context _db;
  
  public ConstructorMethod(string connectionString ="")
  {
      _db = new Project1.Data.Context();
      _db.Database.Connection.ConnectionString = connectionString;
  }
  </code></pre>
  
  <p>It's an DAO class to hold the data access methods and on the constructor I want to pass the connection string but for some reason I can't get it to work. Is there some other way to acheive passing a connection string through the constructor ?</p>
  
  <p>All the posts on this site about this error seem to be unrelated to EF.</p>
  
  <p><strong>EDIT</strong>
  When trying in a seperate project with minimal code. I got the following error :</p>
  
  <blockquote>
    <p>Keyword not supported: 'metadata'</p>
  </blockquote>
  
  <p>That led me to find the solution I'm posting below. I would still like to know why this was happening because it seems to work in MVC 4 applications.</p>