In this tutorial we will see how to enable migrations
Introduction
The Database Migration is a main working part of ASP.NET MVC 4 was released in beta version by the Microsoft ASP.NET MVC developers team.At that time it has come with number of different features.Database Migration using Entity Framework,Mobile version web with the support of JQuery,Web API's.
Database Migration
The Database Migrations is used for Entity Framework and the code first has worked really cool where you can change your codes. Add the migration packages from via package manager.Then update you database as your code.All the database migration changes gets added to source code.
From the above text we have known about what is Database Migration.
Now let us see how it works in the project source code.
Install package Entity FrameWork.
We should add a simple class that represents in your website
public class Product {
public int Id {get;set}
public string Tittle {get;set}
}
In the above mention code that is the example of class.
Now Run the controller recipe and add a new controller to products controller that uses Entity
Framework to the Database.
Enable Database Migration
We can enable the Database Migration from the downloaded packages/ using package manager.
This Database migration creates a new migration folder in the Visual Studio solution when enable is done.
It also creates Initial Create target Migration that has both up migration and down migration.
public partial class InitialCreate : DbMigration {
public override void Up() {
CreateTable(
"Products",
c => new1
{
Id = c.Int(nullable: false, identity: true),
Title = c.String(),
})
.PrimaryKey(t => t.Id);
);
}
public override void Down() {
DropTable("Products");
}
}
Here from the above code the UP migration creates the product table and Down migration drops
the product table.
Addition of new migration Database.
In some cases if we want to create a new migration Db.
Let's say an example on this, We want to add some products to the more properties.We have to mention in the class , Title should be given and length of 500 characters
public class Product {
public int Id { get; set; }
[Required,MaxLength(500)]
public string Title { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
From the above code the next step is ,we can add data migration and we can update the
Database through the package manager
This Data Migration Will creates another file in the migration folder project in MVC 4 application.
The process will update with the update database command with the help of new description
and the price column. The title column will be 500 characters and not nullable.
The new Database Migration File has the appropriate Up and Down Methods.
public partial class AddDescriptionPriceToProduct : DbMigration {
public override void Up() {
AddColumn("Products", "Description", c => c.String());
AddColumn("Products", "Price",
c => c.Decimal(nullable: false, precision: 20, scale: 4));
AlterColumn("Products", "Title",
c => c.String(nullable: false, maxLength: 500));
}
public override void Down() {
AlterColumn("Products", "Title", c => c.String());
DropColumn("Products", "Price");
DropColumn("Products", "Description");
}
}
Conclusion
The Database Migration support in Entity Framework a nice addition to the tooling.
It is supported with Entity Framework 4.3
In this article, we have studied about DATABASE MIGRATION IN ASP.NET MVC 4. Hope you understand.
Reference
www.asp.net