lokopi
lokopi

Reputation: 53

How to create Up and Down Methods using migrations?

I am new to Code first, can you tell me how i can have all Up and Down methods for all tables in the database like below(given for one table)

public partial class abc: DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.UserTasks", "ServiceTechReason", c => c.Long());
        }

        public override void Down()
        {
            DropColumn("dbo.UserTasks", "ServiceTechReason");
        }
    }

i want all three types for a table viz .cs , .Designer.cs , .resx.

2) Can you explain the above example, i pick it from somewhere on internet i was searching for this but found nothing. is abc is my table name in database?

Provide me link if it is already answered.

EDIT

As mentioned by @scheien i already tried those commands they do not automatically override up and down methods for a table

Upvotes: 1

Views: 3176

Answers (1)

schei1
schei1

Reputation: 2487

Creating migrations is done by running the command Add-Migration AddedServiceTechReason.

This assumes that you have already enabled migrations using the Enable-Migrations command.

To apply the current migration to the database, you'd run the Update-Database. This command will apply all pending migrations.

The point of Code-First migrations is that you make the changes you want to your Entity(ies), and then add a new migration using the Add-Migration command. It will then create a class that inherits DbMigration, with the Up() and Down() methods filled with the changes you have made to your entity/entities.

As per @SteveGreenes comment: It does pick up all changes to your entities, so you don't need to run it once per table/entity.

If you want to customize the generated migration files, look under the section "Customizing Migrations" in the article listed.

All these commands are run in the package manager console.

View -> Other windows -> Package Manager Console.

Here's a great article from blogs.msdn.com that explains it in detail.

Upvotes: 1

Related Questions