0

I am working on a VS2012 solution that has got an ASP.NET MVC 4.0 project and multiple class libraries like my Managers, providers and DataAccess projects. My dbContext class is defined inside the DataAccess project.

My aim is to restrict the connection string info in the DataAccess project's App.Config file. I am trying to avoid specifying the connectionString anywhere else in the project as it is my DataAccess classes that would interact with the DB.

Now if I specify my connection string the my dbContext class by hard coding it, my project works fine and is able to read data from DB.


    public MyDbContext()
        : base(@"Data Source=MYLAPTOP\SQL2012MAIN;Initial Catalog=MyDB;User ID=sa;Password=*****")
    {

    }

But if I specify the connection string in app.config file like this:

<connectionStrings>
<add name="MyDBConnection" connectionString="Data Source=MYLAPTOP\SQL2012MAIN;Initial Catalog=MyDB;User ID=sa;Password=*****;Connect Timeout=200; pooling='true" providerName="System.Data.SqlClient" />
</connectionStrings>

and use it in my dbContext classs as follows:

public MyDbContext()
            : base("MyDBConnection")
        {

        }

It doesn't work. I tried using the same connection string in my MVC project's web.config file also, but then again I am getting the same error (attached image):

enter image description here

Can anybody please guide me...

Thanks Hari

Hari
  • 477
  • 1
  • 9
  • 20

2 Answers2

0

I think your fault lies at the pooling, so try Pooling=True also make sure to remove the single inverted comma that you have, '

There is one more point you might want to research on, I think that Pooling is by default enabled, so setting Pooling=True, while this is explicit, if I remember correctly, it has no effect, whereas Pooling=False does have a effect.

Louis Lewis
  • 1,298
  • 10
  • 25
  • Hey Louis, thnx a lot for replying and looking at my issue. That single quote(') was an issue. Once I removed it, the connection string started working as expected in from my web.config file. – Hari Nov 10 '14 at 08:30
  • However, if I use the same conn. string from my app.config file I get the following error: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist." – Hari Nov 10 '14 at 08:34
  • By any chance, do you think since my web.config file doesn't have any connection string field now, my application may be trying to read it from appHost.config or something? The loaclDB reference suggests that it is trying to use the SQLServerExpress – Hari Nov 10 '14 at 08:35
  • You should add the connection string to the web project as well in the web.config. – Louis Lewis Nov 10 '14 at 08:48
0

To make it work you can try calling the base constructor like this:

public MyDbContext()
    : base("MyDBConnection", backend, metadataSource)
{
}

In this case, backend and metadataSource would be the fields in your MyDbContext class, which hold the configuration of the backend and the configuration of the mapping.

In the Creating OpenAccessContext article, you will find more details about the design of the context.