Skip to main content

Entity Framework Migrations

Overview

  1. Create a Modal class
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
}
  1. create migration name InitialCreate
dotnet ef migrations add InitialCreate
  1. Create the database
dotnet ef database update
  1. Update the model class
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
// add new property
public DateTime CreatedTimestamp { get; set; }
}
  1. Create a new migration
dotnet ef migrations add AddCreatedTimestamp
  1. Update the database
dotnet ef database update

Remove Migration

dotnet ef migrations remove

List Migrations

dotnet ef migrations list

Checking for Pending model changes

dotnet ef migrations has-pending-modal-changes

Resetting all migrations

  1. Backup your database
  2. In your database, delete all rols from migration history
  3. Delete Migration folder
  4. Create a new migration script. dotnet ef migrations script
  5. Insert a single row into the migrations history.
INSERT INTO [__EFMigrationsHistory] ([MIGRATIONID], [PRODUCTVERSION])
VALUES (N'<full_migration_timestamp_and_name>', N'<EF_version>');