本文介绍了实体框架数据库初始化:超时初始化新的Azure SqlDatabase时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC应用程序。如果是通过创建一个新客户的 CustomerController 的我运行一个新的后台任务(使用的 HostingEnvironment.QueueBackgroundWorkItem 的)来创建一个新的Azure SqlDatabase该客户。

I have an ASP.NET MVC application. When a new customer is created via CustomerController I run a new background task (using HostingEnvironment.QueueBackgroundWorkItem) to create a new Azure SqlDatabase for that customer.

我使用实体框架code首先创建/初始化新的数据库。这里的code:

I use Entity Framework Code First to create/initialize the new database. Here's the code:

// 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异常的后约40secs。我认为这是因为天青不能这短短的时间期限内初始化新的数据库。不要误会我的意思:数据库将通过Azure的创建很好,但我想等待点/摆脱的 TimeoutException异常

My Problem is that I always get a TimeoutException after about 40secs. I think that happens because Azure cannot initialize the new database within this short period of time. Don't get me wrong: The database will be created well by Azure but I want to wait for that point / get rid of the TimeoutException.

EDIT1:
我使用的连接超时= 300 的在我的ConnectionString但我的应用程序并不真正关心的;经过约40多岁,我一直在运行到SQLERROR。

I'm using Connection Timeout=300 in my ConnectionString but my app doesn't really care about that; after about 40s I'm always running into an SqlError.

EDIT2:
这提出了一个例外是一个的SQLException 。的消息的:超时过期。完成操作或服务器之前已超时期间没有响应。的来源的:.net SqlClient数据提供

The exception that raises is an SqlException. Message: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Source: .Net SqlClient Data Provider

EDIT3:
我现在可以confim,这已经无关ASP.NET/IIS。即使是在一个简单的UnitTest方法上方code失败。

I can confim now that this has nothing to do with ASP.NET/IIS. Even in a simple UnitTest method the code above fails.

推荐答案

这似乎有另一个正在使用code首先迁移时涉及数据库初始化过程的CommandTimeout 设置。我想所以这里分享我的解决方案,以防万一有人遇到过这个问题。

It seems that there is another CommandTimeout setting that is involved in database initialization process when using Code First Migrations. I want so share my solution here just in case anybody encounters this problem too.

感谢的米勒为他的暗示指点我到解决方案。

Thanks to Rowan Miller for his hint pointing me to the solution.

下面是我的code:

// 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类:

And my Configuration.cs class:

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

这篇关于实体框架数据库初始化:超时初始化新的Azure SqlDatabase时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:41