Nirman
Nirman

Reputation: 6793

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

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"?>
<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>
    <contexts>
      <context type="WebExplore.Context.GeographyContext, WebExplore.Context">
        <databaseInitializer type="WebExplore.Context.WebExploreInitializer, WebExplore.Context" />
      </context>
    </contexts>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="WebExploreEntities"
        connectionString="Data Source=.\sqlexpress;Initial Catalog=WebExplore.Database;Integrated Security=true"
        providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

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.

Upvotes: 0

Views: 1385

Answers (1)

Paul-Jan
Paul-Jan

Reputation: 17278

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

Upvotes: 1

Related Questions