EF - Code First approach: connection string and initializer are not being picked from app.config file

app-config code-first entity-framework entity-framework-6 sql-server


I have a class library project where I have created entities, and context for Entity Framework. I have also created an initializer to seed default data in the database.

Following is the app.config file of the class library project -

<?xml version="1.0" encoding="utf-8"?>
    <!-- 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" />
      <context type="WebExplore.Context.GeographyContext, WebExplore.Context">
        <databaseInitializer type="WebExplore.Context.WebExploreInitializer, WebExplore.Context" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameter value="v11.0" />
    <add name="WebExploreEntities"
        connectionString="Data Source=.\sqlexpress;Initial Catalog=WebExplore.Database;Integrated Security=true"

However, when I try to create a database using "Update-Database", it does create a database with a default name, and without seed data.

I believe, somehow the app.config settings are not being picked when I try to create the database through "Update-Database" command.

Can anyone please help me creating database and seed data by taking app.config settings into an account?

Any help on this will be much appreciated.

11/24/2015 8:53:40 AM

Accepted Answer

You are trying to add an app.config to your class library project... you can't (see Can a class library have an App.config file?). You need to add the app/web.config file to a project that can be started (i.e. the actual application).

5/23/2017 12:07:22 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow