Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 6 Code First migrations warn of or prevent destructive updates

Question: Is there a setting to be warned of inadvertent loss of data when running a migration script.

I've been using code first migrations since 4.1, but while experimenting in a test project I ran into behavior I didn't expect. Maybe I haven't kept up on newer features.

Say I have a simple model:

    public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
}

and context:

    public class CustContext : DbContext
{
    public CustContext() : base("DefaultConnection") { }
    public DbSet<Customer> Customers { get; set; }
}

I've already enabled migrations, created the initial migration, updated the database as follows, then populated the table with data.

enable-migrations
add-migration initial
update-database

Then I changed the name of Address to HomeAddress. This is the migration script:

add-migration HomeAddress

    public partial class HomeAddress : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.Customers", "HomeAddress", c => c.String());
        DropColumn("dbo.Customers", "Address");
    }

    public override void Down()
    {
        AddColumn("dbo.Customers", "Address", c => c.String());
        DropColumn("dbo.Customers", "HomeAddress");
    }
}

This is the Configuration script:

    internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstMigrations.Model1.CustContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        AutomaticMigrationDataLossAllowed = false;
    }

    protected override void Seed(CodeFirstMigrations.Model1.CustContext context)
    {

    }
}

When I ran the update-database command, I fully expected to get an error along the lines of "could not complete the migration because it would result in data loss..." I actually see that error a lot working on my own project.

But instead it happily dropped the (populated) Address column, and created a new column HomeAddress.

I figured there must be a configuration setting to control that behavior, but all I could find was AutomaticMigrationDataLossAllowed which apparently only applies to Automatic migrations.

Have I overlooked something?

like image 968
Joel Avatar asked Nov 08 '14 00:11

Joel


People also ask

Which command enables migration in EF 6 first?

To use code-based migrations, first execute the enable-migrations command in the Package Manager Console.

How does EF migrations work?

EF Core compares the current model against a snapshot of the old model to determine the differences, and generates migration source files; the files can be tracked in your project's source control like any other source file. Once a new migration has been generated, it can be applied to a database in various ways.

How do I run EF migrations?

Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.


2 Answers

This has been raised as an issue already but rejected as "by design".

Decided this is by design - we only warn for automatic migrations with data loss.

See the bug here.

like image 132
kjbartel Avatar answered Oct 20 '22 01:10

kjbartel


You can use:

public partial class HomeAddress : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.Customers", "HomeAddress", c => c.String());
        Sql("UPDATE "dbo.Customers" SET HomeAddress = Address");
        DropColumn("dbo.Customers", "Address");
    }

    public override void Down()
    {
        AddColumn("dbo.Customers", "Address", c => c.String());
        DropColumn("dbo.Customers", "HomeAddress");
    }
}

Then you save your data.

like image 28
sribin Avatar answered Oct 20 '22 01:10

sribin