0

My solution consists of two projects. One is a Web API/angular2 application, and the other is a data access layer that interfaces with the entity framework. My web application project references this data layer project in order to create/retrieve data.

I am getting the following error when trying to add new data, or retrieve, from the dbsets in my database context:

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)'

Here's the app config from my data layer project:

    <?xml version="1.0" encoding="utf-8"?>
<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>
  <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>
  <connectionStrings>
    <add name="SettingBuilder" connectionString="Data Source=(LocalDb)\ProjectsV13;Initial Catalog=SettingBuilder;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup></configuration>

And I have replicated this in the web.config on my web api project:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <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>
  <connectionStrings>
    <add name="SettingBuilder" connectionString="Data Source=(LocalDb)\ProjectsV13;Initial Catalog=SettingBuilder;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

The DB does exist - when I try to add-migration or update-database from the package manager console there is no problem and these succeed.

What is the reason I cannot connect to the DB from the Web API project, but have no issues doing so from the console?

Here is the structure of my DB context class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace SettingBuilder.Data
{
    public class SettingBuilderContext : DbContext
    {
        public DbSet<Setting> Settings { get; set; }

        public SettingBuilderContext() //: base("SettingBuilder")
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Setting>().ToTable("Setting");
        }
    }
}

I am following a unit of work pattern - to spare pasting all the code here, instantiating a unit of work also instantiates the db context. Here's a look at that from the controller in my web api:

[HttpPost("[action]")]
public void CreateSetting([FromBody]Setting setting)
{
    _unitOfWork.SettingRepository.CreateSetting(setting);
    _unitOfWork.Save();
}

Any help would be greatly appreciated.

EDIT: I have made a simple console app to test this. From the console app, it works - I have just copied the app.config from my data layer project. However, the DB has been created in a different place with a fully qualified name, not the one I specified in the app.config (it created SettingBuilder.Data.SettingBuilderContext as a DB)

This leads me to assume the framework is not referencing my app config file. Is there a way to configure this?

EDIT 2: This also works if I manually hard code my connection string. I just copied it from the webconfig into the db context initialisation.

  • 1. You dont need app config from your data layer project because its not an executable. Its the WebApi project's config file - web.config is used. 2. Why is 'base("SettingBuilder")' commented out? How EF knows which connection string to use otherwise? 3. Your connection string for EF looks not right.http://stackoverflow.com/questions/5781059/connection-strings-for-entity-framework – Yawar Murtaza Apr 06 '17 at 13:23
  • The config exists in the data layer for use by the package manager console for when I want to add migrations etc. I have tried with that line commented out/not commented out but seems to make no difference. – user3261018 Apr 06 '17 at 13:38
  • I've just updated the post with my findings from developing a console app – user3261018 Apr 06 '17 at 13:41
  • How do you instantiate SettingBuilderContext? – Win Apr 06 '17 at 13:47
  • Is this ASP.Net Core WebApi project? – Yawar Murtaza Apr 06 '17 at 13:48
  • private SettingBuilderContext _context = new SettingBuilderContext(); within the UnitOfWork – user3261018 Apr 06 '17 at 14:01
  • This is indeed an ASP.net core WebApi, targeting .net 452 – user3261018 Apr 06 '17 at 14:01

0 Answers0