Web.config configuration for EF Migrations

I am implementing Code-First Migrations on my project and want to not have to use Add-Migration and Update-Database all the time in the Package Manager Console. I'd like for the updates to the database to happen automatically.

For this reason I updated my web.config file to do this. I deleted my database, but for some reason when I fire up the application, it does not create the database. Am I doing something wrong?

Here's my web.config

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <contexts>
      <context type="AuctionService.DataAccess.AuctionContext, AuctionService.DataAccess">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[AuctionService.DataAccess.AuctionContext, AuctionService.DataAccess], [AuctionService.DataAccess.Migrations.Configuration, AuctionService.DataAccess]], EntityFramework">
        </databaseInitializer>
      </context>
    </contexts>
  </entityFramework>

EDIT

My code is in a WCF service and when I debugged this service I got this

The server encountered an error processing the request. The exception message is 'Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.'


Try ensuring you have set the AutomaticMigrationsEnabled property to true in the constructor of your AuctionService.DataAccess.Migrations.Configuration class.

Once that is going, you may also get an exception if there's potential data loss; you can ignore this by setting the AutomaticMigrationDataLossAllowed property to true .

For example:

internal sealed class Configuration : DbMigrationsConfiguration<AuctionContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        // AutomaticMigrationDataLossAllowed = true; // You may want this also, but be careful.
    }
}

I'm not sure if you can set this in the .config file (can't find documentation on this).

I believe that when you initially set up migrations, you can get the generated Configuration class to have that property set for you by running the command Enable-Migrations –EnableAutomaticMigrations

链接地址: http://www.djcxy.com/p/90250.html

上一篇: 。净

下一篇: EF迁移的Web.config配置