0

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.

Nirman
  • 6,715
  • 19
  • 72
  • 139

1 Answers1

1

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

Community
  • 1
  • 1
Paul-Jan
  • 16,746
  • 1
  • 63
  • 95
  • you mean, when we use code first approach, should we always write our DbContext in the actual application only? I guess, this is opposite to Database-first approach where we can just create the EDMX in the class library and the class library project then contains the connection details to enable updating entities from the database. – Nirman Nov 24 '15 at 09:13
  • 1
    No, the DbContext lives in the Class Library, it's just the .config that lives with the actual application. You can use the same DbContext+ClassLibrary from multiple projects with different connection settings. This is very useful for things like unit testing. – Paul-Jan Nov 24 '15 at 12:27
  • Thanks, I got the point... yes, indeed, I just had to move the configurations in a project that can be started.. thanks – Nirman Nov 24 '15 at 18:37