跳到主要內容

發表文章

目前顯示的是有「code first」標籤的文章

Code First Migration: 什麼是Automatic Migration

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>.AddOr...

MigrateDatabaseToLatestVersion: 發布程式時同時更新資料庫

若想要在發布程式的時候連同資料庫一起更新那該如何作? 方法一 在Global.asax檔案裡, 找到protected void Application_Start 加入 initializer MigrateDatabaseToLatestVersion 如下所示    protected void Application_Start()  {     Database.SetInitializer(new MigrateDatabaseToLatestVersion<AvaBotDataContext, Configuration>());     new AvaBotDataContext().Database.Initialize(true); 加入之後, runtime時在資料庫初始化的階段, initializer會去查資料庫中的migration紀錄, 當程式裡的migration比較新就會去套用他來更新資料庫 方法二 直接在發布時勾選 Execute Code First Migration 他的效果跟方法一一樣, 只是不需要額外寫code而是透過config的方式去告訴initializer作跟MigrateDatabaseToLatestVersion一樣的事

[解決] Update-database 時遇到錯誤訊息: Cannot open server requested by the login. Client with IP address is not allowed to access the server.

簡單的描述一下遇到的狀況 Azure上面有一個我新建的Database, 當我試著使用Entity Framework Code Migration去更新資料的時候, add-migration initialModels update-database visual studio 卻拋出以下的錯誤訊息 Cannot open server 'avbotdata' requested by the login. Client with IP address '108.123.1.251' is not allowed to access the server.  To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range. It may take up to five minutes for this change to take effect. 原因很明顯, 我們想要存取資料庫的時候, Azure 認為發出請求的IP不在Whitelist裡面, 所以擋住了update的請求 解法其實很簡單 Step 1. 選擇你要設定的資料庫, 在搜尋列上輸入 Firewall Step 2. 輸入你要加入的IP後存檔