jeudi 6 avril 2017

Entity Framework 6 - Connection can be made from package manager but not from Web API project

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://ift.tt/1eigFsq -->
    <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.




Aucun commentaire:

Enregistrer un commentaire