2016-03-03 90 views
5

我有一個ASP.NET MVC應用程序。當通過CustomerController創造了一個新的客戶我運行一個新的後臺任務(使用HostingEnvironment.QueueBackgroundWorkItem)創建一個新的Azure的SqlDatabase該客戶。實體框架數據庫初始化:初始化新的Azure SqlDatabase時超時

我使用實體框架代碼首先創建/初始化新的數據庫。下面的代碼:

// My ConnectionString 
var con = "..."; 

// Initialization strategy: create db and execute all Migrations 
// MyConfiguration is just a DbMigrationsConfiguration with AutomaticMigrationsEnabled = true 
Database.SetInitializer(strategy: new MigrateDatabaseToLatestVersion<CustomerDataContext, MyConfiguration>(useSuppliedContext: true)); 

using (var context = new CustomerDataContext(con)) 
{ 
    // Neither 'Connection Timeout=300' in ConnectionString nor this line helps -> TimeoutException will rise after 30-40s 
    context.Database.CommandTimeout = 300; 

    // create the db - this lines throws the exception after ~40s 
    context.Database.Initialize(true); 
} 

我的問題是,我總是得到TimeoutException異常後約40秒。我認爲這是因爲Azure無法在短時間內初始化新的數據庫。不要誤會我的意思:Azure會很好地創建數據庫,但我想等待那個點/擺脫TimeoutException

EDIT1: 我使用連接超時= 300在我的ConnectionString,但我的應用程序並不真正關心的是;在大約40年後,我總是碰到一個SqlError。

EDIT2: 這提出了一個例外是SQLEXCEPTION消息:超時已過期。操作完成之前超時的時間或服務器沒有響應。 來源:.net SqlClient數據提供

EDIT3: 我現在可以confim,這已經無關ASP.NET/IIS。即使在一個簡單的UnitTest方法中,上面的代碼也會失敗。

+0

你能分享你所看到的實際例外,完整的錯誤消息?謝謝! –

+0

我已添加例外。謝謝。 – mmmato

+0

您的遷移配置類中還有一個CommandTimeout屬性,您可以嘗試設置該屬性。 https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrationsconfiguration.commandtimeout(v=vs.113).aspx –

回答

6

在使用Code First Migrations時,似乎還有另一個CommandTimeout設置涉及數據庫初始化過程。我想在這裏分享我的解決方案,以防萬一有人遇到這個問題。

感謝Rowan Miller他的提示指向我的解決方案。

這裏是我的代碼:

// Initialisation strategy 
Database.SetInitializer(strategy: new CreateDatabaseIfNotExists<MyDataContext>()); 

// Use DbContext 
using (var context = new MyDataContext(myConnectionString)) 
{ 
    // Setting the CommandTimeout here does not prevent the database 
    // initialization process from raising a TimeoutException when using 
    // Code First Migrations so I think it's not needed here. 
    //context.Database.CommandTimeout = 300; 

    // this will create the database if it does not exist 
    context.Database.Initialize(force: false); 
} 

而且我Configuration.cs類:

public sealed class Configuration : DbMigrationsConfiguration<MyDataContext> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = false; 
     AutomaticMigrationDataLossAllowed = false; 

     // Very important! Gives me enough time to wait for Azure 
     // to initialize (Create -> Migrate -> Seed) the database. 
     // Usually Azure needs 1-2 minutes so the default value of 
     // 30 seconds is not big enough! 
     CommandTimeout = 300; 
    } 
} 
1

命令超時和連接超時是兩個不同的設置。在這種情況下,你只能增加commandtimeout。您可以增加web.config中的連接超時值:Connection Timeout=120。您想要增加連接超時的唯一時間是在創建數據庫時。

+0

感謝您的提示,但不幸的是,這並沒有改變任何東西。我的ConnectionString已經在使用Connection Timeout:Server = XXX.database.windows.net; Database = DB_Customer_34; User Id = XXX; Password = XXX; Connection Timeout = 240; MultipleActiveResultSets = True; Encrypt = True; App = EntityFramework – mmmato