djblois
djblois

Reputation: 815

Entity framework code first migration coming up blank

I am using entity framework to attach to an existing database where I will add a few more tables. Someone on here said this is not possible and I would need to keep the new tables separate in a new database. Here is that question:

Do not create existing table during Migration

I did some more investigation and found this on MSDN:

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

According to this I should run an initial migration like this:

add-migration initial -ignorechanges

so I did that and that is supposed to look at the database and match it up. After I update the database, then I am supposed to add another migration without the -ignorechanges. When I do the second migration, I get this:

namespace PTEManager.Domain.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class second : DbMigration
    {
        public override void Up()
        {
        }

        public override void Down()
        {
        }
    }
}

so it is not trying to add the 2 new tables and relationships that I need. It is coming up blank. I have tried deleting the _migrationhistory table from the database and starting over but still nothing. What am I missing?

Upvotes: 0

Views: 1880

Answers (2)

Coding Flow
Coding Flow

Reputation: 21881

It sounds like you are adding the second migration without making any changes. What you need to do is this:

  • Add DBSet<ModelName> properties to your context for all existing tables.
  • Create the initial migration using -ignorechanges
  • Add DBSet<ModelName> properties to your context for all new tables.
  • Create the second migration as normal.

The second migration should then contain code to create only the new tables, relationships etc. you want. It doesn't matter whether you update the database in between migrations or only once at the end.

Upvotes: 2

Hezye
Hezye

Reputation: 1551

You could try to add normal migration and modify Up/Down methods so they include only the 2 new tables.

Upvotes: 0

Related Questions