Add-Migration while there are explicit migrations pending

23,170

Solution 1

I would use the following approach.

First you need to apply migrations and specify the last one.

PM> Update-Database -TargetMigration AnyMigrationName

# It updates database to a migration named "AnyMigrationName"
# This will apply migrations if the target hasn't been applied or roll back migrations
# if it has

When your local database is updated you simply add new migration.

PM> Add-Migration NewMigrationName

# it scaffolds a new migration named "NewMigrationName" 

Looking for a good reference I found these blog posts about EF Migrations that can help you a lot. They cover many questions and EF Migrations and a Merge Conflict and EF Migrations Command Reference in particular.

Solution 2

this error is shown when you have many Migrations pending.

If you don't need existing migrations, first delete (exclude) all migration on Migration folder and then add a new migration.

Add-Migration new

Share:
23,170
Fernando Espinosa
Author by

Fernando Espinosa

Fervent evangelist of cloud-native tooling, practices & patterns such as Kubernetes, Service Mesh, Microservices Architecture, Infrastructure-as-Code, DevOps/NoOps/GitOps, Continuous-Delivery, Serverless Computing (Lambda, Knative, FaaS), Event-Driven Design (CQRS, Event Sourcing, PubSub), NoSQL (Cassandra, DynamoDB, MongoDB), Domain-Driven Design, Test-Driven Development, Object-Oriented, Functional and Reactive programming. Graduated from University of Toronto with double Honours B.Sc. degree in Mathematics and Applied Mathematics, he has enjoyed 15+ years of successful experience leading teams in all aspects of software engineering, architecture and delivery of highly available distributed systems, server-side and client-based enterprise applications. He is also the founder of Sports Artificial Intelligence Corporation and certified as a Level 3 Ski Instructor by the Canadian Ski Instructor's Alliance. ❖ Microservices, Containerization & Service Discovery: Docker • Kubernetes • Helm • Fluentd • Prometheus Istio • Eureka • Consul • Envoy AWS EKS • AWS Fargate • AWS ECS • Azure AKS ❖ Infrastructure-as-Code: Terraform • CloudFormation • Ansible • Packer • Azure Resource Manager ❖ Programming Languages & Frameworks: JVM: Java • Scala • Kotlin • Groovy • Spring • Akka Golang • GraphQL • Protobuf Python • Jupyter • NumPy • pandas • Matplotlib Node.js • TypeScript • ES6 • Webpack React • Redux • Angular • RxJS C# • .NET Core • ASP.NET • MVC • Web API ❖ Stream Processing & Data Engineering: Apache Kafka • Spark • AWS Kinesis • Akka Streams ❖ NoSQL & RDBMS: Apache Cassandra • MongoDB • AWS DynamoDB • Redis • etcd MS SQL Server • PostgreSQL • MySQL • AWS RDS ❖ Event-Driven & Asynchronous Architecture: CQRS • Event Sourcing • PubSub Service Bus • Event Hub ❖ Serverless Computing: AWS Lambda • Aurora Serverless • Azure Functions ❖ CI/CD: GitHub • TeamCity • Jenkins • Azure Pipelines CircleCI • Octopus Deploy • Spinnaker

Updated on December 03, 2020

Comments

  • Fernando Espinosa
    Fernando Espinosa over 3 years

    So I have a basic problem trying to generate a migration when I don't have a DB in sync (up to the latest migration, in fact, have no DB at all) and have already made substantial changes to my model.

    This are the specific details:

    1. Have a sequence of explicit migrations.
    2. Have no database. In short, have lots of pending changes.
    3. Made changes to the models/context.
    4. When trying to Add-Migration, it will complain saying precisely that explicit migrations are pending.
    5. If I try to execute the migration (and bring my DB in sync), either via Update-Database or migrate.exe, it will fail (after applying explicit migrations successfully), and automatic migrations are not enabled.
    6. I don't want to enable automatic migrations either (to prevent any schema changes to be committed and go unaccounted in code.)

    I realize that after the failure in #5 I can now run #4. Then re-try #5 and voilá.

    My Question is if this is the expected approach to solve this silly situation.

  • Fernando Espinosa
    Fernando Espinosa over 9 years
    Neat. Makes sense... Thanks!
  • Jeroen
    Jeroen over 6 years
    Had the same problem, turned out my new migration wasn't added to source control. Stupid mistake