2

I've noticed that when I create model-first database using EF it is ignoring web.config connection strings and doing it's own thing in the background. Then I found a way to set my own connection string in constructor like so.

public class MainDataContext : DbContext
{
    public MainDataContext()
    {
        this.Database.Connection.ConnectionString = "MY CONNECTION STRING THAT IS ACTUALLY USED";
    }
}

Question: How can I force/set it to use connection string from web.config?

I've been told that if you name your connection string just like your data context it will work but it doesen't for me. I know for sure that it does not work if I name it DefaultConnectionString.

<add name="MainDataContext" providerName="System.Data.SqlClient"
     connectionString="Server=(LocalDB)\\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|ProjectDB.mdf;" />

Error occurred when I try to do update-database -v -f from console.

Stan
  • 25,744
  • 53
  • 164
  • 242
  • which version of EF you are using? – MMK Nov 28 '12 at 16:07
  • @MMK 5.0? The one that comes with VS2012 (MVC4) – Stan Nov 28 '12 at 16:08
  • try something like public MainDataContext() :base(ConfigurationManager.ConnectionStrings["MainDataContext"]) { } – MMK Nov 28 '12 at 16:12
  • @MMK I get this error man `cannot convert from 'System.Configuration.ConnectionStringSettings' to 'System.Data.Entity.Infrastructure.DbCompiledModel` :| – Stan Nov 28 '12 at 16:23
  • try this: public MainDataContext() :base("MainDataContext") { } – MMK Nov 28 '12 at 16:55

1 Answers1

1

You can try something like this:

public MainDataContext() :
        base(typeof(MainDataContext).Name)
    {
    }

Are you sure your connection string declaration in the Web.config is correct? Like this:

<?xml version="1.0"?>
<configuration>
<connectionStrings>
    <add name="MyDatabase" connectionString="server=localhost;User Id=user;password=password;Persist Security Info=True;database=mydatabase" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
Arthur Nunes
  • 6,718
  • 7
  • 33
  • 46
  • Still same thing. When I add `this.Database.Connection.ConnectionString = "Server=(LocalDB)\\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|ProjectDB.mdf;";` inside of constructor it works. – Stan Nov 28 '12 at 16:17
  • Yes, 100% positive. My connection string just looks a bit different (see op post) but all I did was edited the name of `DefaultConnectionString` and edited the connection string itself. – Stan Nov 28 '12 at 16:28
  • Just to check: is your custom dbcontext declared in a library and the connection string is in a app.config file of the library? Because if it is, you have to copy the connection string configuration to the web.config/app.config of the application that is going to use your data library. I know you mentioned the web.config, but the problem seems to be something of the kind, configuration declared in the wrong file. – Arthur Nunes Nov 28 '12 at 16:37
  • See http://stackoverflow.com/questions/8693492/get-connectionstring-from-app-config-in-c-sharp?lq=1. Check, in your context constructor, if the connection string is acessible: string connectionString = ConfigurationManager.ConnectionStrings["CString"].ConnectionString; Like @MMK suggested, but add a ".ConnectionString" – Arthur Nunes Nov 28 '12 at 16:52
  • Problem was that I was escaping the escape key in my string (`(LocalDB)\\v11.0`) but when it took it from `web.config` it escaped my escaping the string and it turned to `(LocalDB)\\\\v11.0`. Your solution works just fine. Thanks. – Stan Nov 28 '12 at 17:16