SQL Server Express and Entity Framework

connection-string entity-framework sql-server-express

Question

I'm new to SQL Server Express (not to SQL Server in general) and I have a problem. I'm using Entity Framework (6-beta) in an ASP.NET MVC application.

I created a database using the SQL Server Management Studio. I can connect to the DB using Management Studio and my local Windows user account. I dind't change the default configuration, so the SQL Server stores the data files in :

C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA

The .mdf file of my database is stored in this folder.

My problem is: When I start the MVC application from Visual Studio, Entity Framework failes to open a connection to the database. The exception text is quite unprecise, but the inner exception reveals that it tries to connect to:

C:\Users\<MyUser>\Workspaces\<Solution>\<Project>\App_Data\<Context>.mdf

My connection string is:

"Data Source=.\SQLEXPRESS; Initial Catalog=<Name>; User Instance=False; Integrated Security=True;"

Does anyone know why Entity Framework tries to connect to this file? What can I do?

1
2
7/23/2013 8:52:35 AM

Accepted Answer

Ok. I found the problem. I edited the wrong web.config file by accident. Asp.Net MVC 5 creates a second web.config file in the Views folder. Of course, the connection string must be inserted in the web.config of the root folder.

2
7/23/2013 8:17:19 PM

Popular Answer

You need a web.config entry on the MVC application project (in case the DBContext is in another referenced project).

The web.config should have an entry like the one below in the connectionstrings section (note YOURCONTEXTNAME should match the name of your DBContext class).

  <add name="YOURCONTEXTNAME" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YOURDATABASENAME;Integrated Security=SSPI"
       providerName="System.Data.SqlClient" />


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow