5

私はASP.NET MVCアプリケーションを持っています。新しい顧客がによって作成された場合、新しいバックグラウンドタスク(HostingEnvironment.QueueBackgroundWorkItemを使用)を実行して、その顧客の新しいAzure SqlDatabaseを作成します。Entity Frameworkデータベースの初期化:新しいAzure SqlDatabaseを初期化するときのタイムアウト

私はEntity Framework Code Firstを使用して新しいデータベースを作成/初期化します。ここでは、コードです:

// 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); 
} 

私の問題は、私はいつもおよそ40secs後TimeoutExceptionを得るということです。私はAzureがこの短い時間内に新しいデータベースを初期化できないために起こると思います。私を間違えないでください:データベースはAzureによってうまく作成されますが、その時点で待つ/ タイムアウト例外を取り除きたいです。

EDIT1: 私は私のConnectionStringで= 300接続タイムアウトを使用していますが、私のアプリは本当に気にしません。約40秒後、私は常にSqlErrorを実行しています。

EDIT2: SqlExceptionがで発生させた例外。 メッセージ:タイムアウトが切れています。操作が完了する前にタイムアウト時間が経過したか、サーバーが応答していません。 出典:.NET SqlClientデータプロバイダ

EDIT3: 私は、これはASP.NET/IISとは何の関係もないことになりましconfimことができます。単純なUnitTestメソッドであっても、上記のコードは失敗します。

+0

あなたは、完全なエラーメッセージが表示されて、あなたが見ている実際の例外を共有することはできますか?どうも! –

+0

私は例外を追加しました。ありがとう。 – mmmato

+0

マイグレーション設定クラスにCommandTimeoutプロパティもあります。設定することはできますか? https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrationsconfiguration.commandtimeout(v=vs.113).aspx –

答えて

6

コードファーストマイグレーションを使用すると、データベースの初期化プロセスに関係する別の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

コマンドのタイムアウトと接続のタイムアウトは、2つの異なる設定です。この場合、コマンドタイムアウトを増やすだけです。 web.config:Connection Timeout=120で接続タイムアウトを増やすことができます。接続タイムアウトを増やしたいのは、データベースを作成するときだけです。

+0

あなたのヒントはありがたいですが、残念ながらそれは何も変わりません。私のConnectionStringは既にConnection Timeoutを使用しています:Server = XXX.database.windows.net;データベース= DB_Customer_34;ユーザーID = XXX;パスワード= XXX;接続タイムアウト= 240; MultipleActiveResultSets = True; Encrypt = True; App = EntityFramework – mmmato

関連する問題