Entity Framework 6 with MVC5 on Visual Studio 2017

I have been going through a fairly large tutorial on MVC with Entity Framework.  All the lessons contained within have been working fine as have other tutorials.  But the same problem has occurred once I try to change the data source from the VS2017 app_data mdf file to a connection to a local SQL instance.    The code fails, either cannot locate the server or that the database is in uses.

I do not have an SQL Server installed locally, and so I assume that the external to the VS environment server is SQLExpress.  I have tried from the package manager console Add-Migration SomeName  followed by Update-Database.  The Update-Database fails sometimes and the initial pull of a set of rows for the start of the application fails other times telling me that either the table isn't there or that it cannot be dropped etc.  A nightmare of problems created the moment I try to use a connection string in my web.config.

I will include the contents of the web.config here and hopefully someone will have some ideas as to how to fix this.

<?xml version="1.0" encoding="utf-8"?>
  For more information on how to configure your ASP.NET application, please visit
    <!-- 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=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <add key="webpages:Version" value="" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
    <add name="OrThisOneSchoolContext" connectionString="Data Source=(localdb)\\MSSQLLocalDb;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
    <validation validateIntegratedModeConfiguration="false" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />

      <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
        <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameter value="mssqllocaldb" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

Open in new window

Howard BashSenior Software EngineerAsked:
Who is Participating?
David Johnson, CD, MVPOwnerCommented:
from the server panel in Visual Studio you must be able to connect to the remote server.  You may have to use a sql login i.e. sa account and the firewall must have port 1433 open to your computer's ip address/range
Howard BashSenior Software EngineerAuthor Commented:
I don't have a remote SQL Server to connect to.  I have SQL Management Studio and the SQLExpress that I believe gets installed with Vs2017.

I would think I want to use something like .\SQLExpress as the SQL Server in the connection string.
David Johnson, CD, MVPOwnerCommented:
sql express does not get installed with vs2017 nor does sql management studio
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Howard BashSenior Software EngineerAuthor Commented:
  • I thought it does.  What is the SQL Server instance I see in VS?
  • Should I download SQLEXpress 2017?
David Johnson, CD, MVPOwnerCommented:
yes you should download and install Sql Express.  What you are seeing is localDB (probably)
I don't have sqlexpress but a sql server on my machine here
sql express not showing
Howard BashSenior Software EngineerAuthor Commented:
I have added SQLExpress and updated the connection string for the project and all is working!
David Johnson, CD, MVPOwnerCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.