2

I have a solution project in in which I have implemented Entity framework in a class library Project. I have a App.config file in the same class library in which I am giving my connection string as

<connectionStrings>
<add name="DefaultConnection"
        connectionString="Data Source=DDC5-D-4R03T72;Initial Catalog=ServiceAdaptor;User ID=sa;Password=pwd;MultipleActiveResultSets=True;Integrated Security=False"

      providerName="System.Data.SqlClient"/>
 </connectionStrings>

(I am using Visual Studio 2010, and I am using Code-First Approach EF)

Now the problem is when I run command "Update-Database -Verbose", It is not creating tables in above DB , rather it creates in .\SQLExpress which I didn't mentioned in anywhere in my soln)

when I gave command like

Update-Database -Verbose -ConnectionStringName "DefaultConnection"

it gave me message

"No connection string named 'DefaultConnection' could be found in the application config file."

What I conclude is my Update-Database command is not able to get the connection string from config file.

Not able to figure out,What is wrong.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sharad
  • 435
  • 1
  • 5
  • 18

1 Answers1

1

Finally I found the solution, There Problem was here that somehow Update-database command was searching Connection string in Web.Config file not in App.Config file as in my Case I have Class library project which has App.config file.

Since It did't get the Connection string By default EF framework in VS 10 will assume .\SQLEXPRESS as default db and create the tables there.

So the solution I found is:

1) Add reference System.Configuration in the project

2) Insteed of using base("DefaultConnection") in dbContext use as below

base(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)

Sharad
  • 435
  • 1
  • 5
  • 18
  • So your connectionstring was in your startup project, sounds to me like I was right after all... – Alexander Derck Dec 17 '15 at 18:14
  • Thanks Alex for your quick efforts but it was not that case as u said.. Issue was not with the startup project config file.. It was in the same class lib project in which my context was implemented. Issue was the class lib project was having connection string which was not identified by Update-database command. We need to make some changes in order to get it from App.config.If it is web.config it works fine without any issue. – Sharad Dec 17 '15 at 18:40