How to pass a EF DB First connection string via constructor instead put in app.config

I'm developing a DataBase First EF application and everything is working great with the auto-generated code and app.config sections. But when I try to remove the connection string of the app.config and put it as parameter on the DbContext constructor I cannot get it to work.

The connection string in the app.config:

  <add name="EntityContext" 
       connectionString="metadata=res://*/ModelHistory.csdl|res://*/ModelHistory.ssdl|res://*/ModelHistory.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string=&quot;DATA SOURCE=XXXXX;PASSWORD=xxxxx;PERSIST SECURITY INFO=True;USER ID=XXXX&quot;" 
       providerName="System.Data.EntityClient" />

And im trying to pass it via constructor:

public EntityContext() : base(GetConnectionString()) { }

private string GetConnectionString()
    return metadata=res://*/ModelHistory.csdl|res://*/ModelHistory.ssdl|res://*/ModelHistory.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string=&quot;DATA SOURCE=XXXXX;PASSWORD=xxxxx;PERSIST SECURITY INFO=True;USER ID=XXXX&quot;" 

But it throws the error:

Additional information: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.


Here is the answer provided by the EF team for EF6 and it has worked for me.

Need Your Help

Building argv and argc

qt arguments main argv argc

I'm a student programmer using Qt to build a GUI application for work. The primary purpose of this application is to open some of our old style files, allows better editing and then save the file i...

How do I debug this JavaScript error?

javascript html wordpress slider

I am working on a staging site, and the homepage slider isn't work. I'm new to debugging JavaScript, how do I break down, with Firebug, or using the console, why the slider isn't working?