Automatic Migration 這個功能可以在Configuration.cs中設定
若下所式
internal sealed class Configuration : DbMigrationsConfiguration<AcerVoiceAssistant.Models.AvaBotDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(AcerVoiceAssistant.Models.AvaBotDataContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
或是在Enable Migration 時代參數如下
enable-migrations –EnableAutomaticMigration:$true
那Automatic Migration有什麼作用呢?
通常要作Migration首先要執行Add Migration, 把目前跟資料庫中的差異寫到DbMigration裡面(如下)
public partial class Init : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Activities",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
FromId = c.String(),
FromName = c.String(),
RecipientId = c.String(),
RecipientName = c.String(),
TextFormat = c.String(),
TopicName = c.String(),
HistoryDisclosed = c.Boolean(nullable: false),
Local = c.String(),
Text = c.String(),
Summary = c.String(),
ChannelId = c.String(),
ServiceUrl = c.String(),
ReplyToId = c.String(),
Action = c.String(),
Type = c.String(),
Timestamp = c.DateTimeOffset(nullable: false, precision: 7),
ConversationId = c.String(),
})
.PrimaryKey(t => t.Id);
然後在用Update-Database來套用更新
但啟動Automatic Migration之後可以直接省掉 Add Migration, 直接修改映射的model後, Update-Database會直接套用改變
是一個在初期開發階段非常好用的功能
缺點: 因為省掉了Add Migration的動作所以不會有migration的紀錄
留言
張貼留言