2

Inside my DAL project I have repositories which I consume from ui. I've refactor project a bit and now on gather data from repository I'm getting error

"No connection string named 'MyAppDbContext' could be found in the application config file."

My App.config on same project (DAL) where repository lives have following context

<?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>
  <connectionStrings>
    <add name="MyAppDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyApp.DAL.MyAppDbContext;Integrated Security=True;AttachDBFilename=MyApp.DAL.MyAppDbContext.mdf" providerName="System.Data.SqlClient" />               
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

I actually have two questions:

  • Why I'm getting this error message: "No connection string named 'MyAppDbContext' could be found in the application config file."
  • How can I set path in conn. string to use db from App_Data directory (after I move it there ofcourse)
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1765862
  • 13,635
  • 28
  • 115
  • 220

1 Answers1

1

Try adding connection string to the configuration file of the consuming project, not the DAL project.

Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
  • This is so weird. It worked for me too but why? And what if I only have one project, the data model definitions, and no other main, executable project? –  Jan 30 '16 at 21:09
  • @AndyJ, Not sure but it is what it is. Some executable or main project is needed to load data model and execute code, and configuration comes from that by default. Your data model cannot execute itself, so that case cannot exist! – Mehrzad Chehraz Jan 31 '16 at 15:27