4

I am having the architecture like this : enter image description here

Where MVC layer is the presentation layer. EF is class library and Repository is another class library.I am trying to insert data to database from repository by creating the EF context object. Added EF reference into Repository class library. EF having the edmx file. its app.config having the connection string generated by EF. code is :

public bool CreateUser(User _user)
   {
       context.Users.Add(_user);
       context.SaveChanges();
       return true;
   }   

but while executing this I am getting following exception :

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

I tried to add same connection string with same name in repository app.config. but not working. anyone have solution ?

Edited: connection string is :

<add name="MyEntitiesConnection" connectionString="metadata=res://*/EF.Entities.csdl|res://*/EF.Entities.ssdl|res://*/EF.Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Servername\MSSQL2008R2;initial catalog=MyDBName;persist security info=True;user id=sa;MultipleActiveResultSets=True;App=EntityFramework;" providerName="System.Data.EntityClient" />

app.config:

<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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
    <connectionStrings>
        <add name="MyEntitiesConnection" connectionString="metadata=res://*/EF.Entities.csdl|res://*/EF.Entities.ssdl|res://*/EF.Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Servername\MSSQL2008R2;initial catalog=MyDBName;persist security info=True;user id=sa;MultipleActiveResultSets=True;App=EntityFramework;" providerName="System.Data.EntityClient" />
    </connectionStrings>    
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>
Red Swan
  • 15,157
  • 43
  • 156
  • 238

1 Answers1

2

In any .NET application, only one config file is the natural starting point for looking for configuration information. For web applications, that's the web.config file at the root of the application1.

Whilst you may have a file called app.config in your repository project (and, indeed, some VS tooling may have added one) or your EF project, it's not used when you try to read configuration information.

The connection string section needs to exist in the web.config of your MVC app.


1For non-web applications, it's the app.config for the project that produces the .exe file and that gets automatically copied as XXX.exe.config during the build.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • ok so does it means I cannot use EF in separate class library? since If we want to use EF as dll across the layers ? – Red Swan May 02 '14 at 06:42
  • 1
    You can use it in a separate class library. But you have to copy the connection string information into the `web.config` of the application that's *using* that class library. Class libraries don't get their own, separate `config` file. – Damien_The_Unbeliever May 02 '14 at 06:44
  • That is what, I am using EF dll in Repository project. I tried to add same connection string in Repository's app.config. but same issue – Red Swan May 02 '14 at 06:45
  • I don't know how else to say this. Those `app.config` files in class library projects are meaningless. Only the `web.config` file matters. – Damien_The_Unbeliever May 02 '14 at 06:46
  • but since Repository class library using context of EF class library, then It should use app.config setting of EF app.config configuration rather running context project. – Red Swan May 02 '14 at 07:06
  • **NO**. Again. Those `app.config` files that you have in your class library projects are meaningless. They are *not* used at all. Whenever you access config, no matter whether you're in the main application project or in a class library's code, the config file that is read is the config file for the application - the `web.config` for a web application or the `XXX.exe.config` file for any other type of application. Just put your connection information into the `web.config` file of your MVC application and you'll find that the error goes away. – Damien_The_Unbeliever May 02 '14 at 07:37
  • worked for me to add connection string in web.config – Red Swan May 02 '14 at 12:58