Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 400
Sachin Kainth Avatar asked Nov 03 '12 22:11

Sachin Kainth


People also ask

How do I run EF core migrations?

Install the tools First, you'll have to install the EF Core command-line tools: We generally recommend using the . NET Core CLI tools, which work on all platforms. If you're more comfortable working inside Visual Studio or have experience with EF6 migrations, you can also use the Package Manager Console tools.

How do I run down my EF migration?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.


1 Answers

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

like image 80
David Moore Avatar answered Oct 21 '22 04:10

David Moore