thedriveee
thedriveee

Reputation: 619

Entity framework code first migration always tries to create new database

I do every step on that video https://www.youtube.com/watch?v=i7SDd5JcjN4

  1. PM> enable-migrations
  2. Change my entity model (add some property)
  3. PM> add-migration migrationName

And there VS generate code for creation database anew. But I need to add just 1 property.

If you need code: 1. Generated with migration:

public partial class alter2 : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Articles",
            c => new
                {
                    Id = c.Int(nullable: false),
                    Content = c.String(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Chapters", t => t.Id)
            .Index(t => t.Id);

        CreateTable(
            "dbo.Chapters",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Title = c.String(),
                    CreationDate = c.DateTime(nullable: false),
                    Level = c.Int(nullable: false),
                    Url = c.String(),
                    Intro = c.String(),
                    Outro = c.String(),
                    MyProperty = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.Id);

    }

    public override void Down()
    {
        DropForeignKey("dbo.Articles", "Id", "dbo.Chapters");
        DropIndex("dbo.Articles", new[] { "Id" });
        DropTable("dbo.Chapters");
        DropTable("dbo.Articles");
    }
}

My entities:

public class Article {

    public int Id {
        get;
        set;
    }

    public string Content {
        get;
        set;
    }

    public virtual Chapter Chapter {
        get;
        set;
    }

}

public class Chapter {

    [ForeignKey ("Article")]
    public int Id {
        get;
        set;
    }

    public string Title {
        get;
        set;
    }

    public DateTime CreationDate {
        get;
        set;
    }

    public int Level {
        get;
        set;
    }

    public string Url {
        get;
        set;
    }

    public virtual Article Article {
        get;
        set;
    }

    public string Intro {
        get;
        set;
    }

    public string Outro {
        get;
        set;
    }

    public int MyProperty {
        get;
        set;
    }

}

EF Context:

public class EFContext : DbContext {

    public EFContext () : base ("name=EFContext") {}

    public virtual DbSet<Chapter> Chapters {
        get;
        set;
    }

    public virtual DbSet<Article> Articles {
        get;
        set;
    }

    protected override void OnModelCreating (DbModelBuilder modelBuilder) {

        modelBuilder.Entity<Article> ()
                    .HasRequired (a => a.Chapter).WithOptional (a => a.Article);

    }

}

Upvotes: 2

Views: 1655

Answers (1)

Steve Greene
Steve Greene

Reputation: 12324

You need an initial baseline migration. EF will always compare new migration to prior, so if you have nothing prior it's going to add code to create objects you already have in the database. So, do this after enabling migrations, but before making your model changes:

Add-Migration InitialCreate –IgnoreChanges
Update-Database

Now your next migration(s) will be incremental.

https://msdn.microsoft.com/en-us/data/dn579398.aspx#option1

Upvotes: 5

Related Questions