0

I have a new ASP.NET web application I am calling "Smartifyer" that was empty to start with. I added EntityFramework with Nuget and have created a model called WordModel and the following context:

public class SmartifyerContext : DbContext
{
    public SmartifyerContext()
        : base("SmartifyerDatabase")
    {

    }
    public DbSet<WordModel> Words { get; set; }
}

Within Web.config in the root directory I have the following configurations:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="SmartifyerDatabase"
       connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SmartifyerDb.mdf;Integrated Security=True"
       providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

When running a test on the context, the Find method takes about 30 seconds until it times out with the following error:

System.Data.SqlClient.SqlException: 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: 26 - Error Locating Server/Instance Specified)

I have a pretty poor understanding of setting up SQL connections and what all the configurations within Web.config do. I would like the .mdf database file to be auto-generated if it is not present but I am unsure how to modify my configurations to do that.

EDIT WITH SOLUTION: All of my connection config and database stuff was setup correctly. My problem was that I was running a unit test which was using the test project's app.config file rather than the main project's web.config. Copy and pasting my web.config into the test project's app.config fixed the issue!

Samuel Davidson
  • 783
  • 6
  • 16
  • I'm pretty sure that EF will create the entities, but not the database, but somebody may say different... – Ron Beyer Jul 17 '15 at 18:26
  • In previous projects using EF I have worked with were setup such that the database would be regenerated if I deleted the `.mdf`. – Samuel Davidson Jul 17 '15 at 18:32

1 Answers1

2

The error you're getting is because the EF is not able to locate the database instance service. The EF have default connection string that connects to developer database known as LocalDB\V11.0. Here your application is trying to connect to LocalDB\V11.0 as specified in ConnectionString.

<add name="SmartifyerDatabase" connectionString="Data Source=(LocalDB)\v11.0; ......

You must ensure that LocalDB instance is installed on you machine. see this answer. If it's not there install it and try to connect manually to the LocalDB instance then try with the application. Hope this will resolve your issue.

For information about the SQL LocalDB see this MSDN article.

You can also use your SqlExpress or other Sql database if it's installed on your machine. Just change the DataSource property in the connectionstring and you will be good to go.

Community
  • 1
  • 1
vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • I figured out the issue. All of my connection stuff was setup correctly. My problem was that I was running a unit test which was using the test project's `app.config` file rather than the main project's `web.config`. Copy and pasting my `web.config` into the test project's `app.config` fixed the issue! – Samuel Davidson Jul 19 '15 at 19:09