asp.net-mvc – 由于基于角色的[Authorize]属性,ASP.NET MVC 5 Azure网站无法在某些视图上连接到SQL Azure DB

我们有一个ASP.NET MVC 5网站,当我们使用Visual Studio在本地运行时,它可以正常工作.在本地运行时,网站连接到两个SQL Express数据库(AspNetUsers和我们的内容数据库).我们使用Entity Framework连接到内容数据库.

我们将网站部署到Azure,并将2个数据库迁移到SQL-Azure数据库.

以下是web.config中的连接字符串

<connectionStrings>    
<add name="AspNetUsersDatabase" connectionString="Data Source=tcp:ZZZ.database.windows.net,
1433;Initial Catalog=ZZZ;User ID=ZZZ@ZZZ;Password=ZZZ;" 
providerName="System.Data.SqlClient" />

<add name="Group5DatabaseConnectionString" connectionString="Data Source=YYY.database.windows.net,
1433;Initial Catalog=YYY;User ID=YYY@YYY;Password=YYY;" 
providerName="System.Data.SqlClient" />

<add name="Group5DatabaseEntities" connectionString="metadata=res://*/Models.Group5Model.csdl|
res://*/Models.Group5Model.ssdl|res://*/Models.Group5Model.msl;provider=System.Data.SqlClient;
provider connection string=&quot;data source=XXX.database.windows.net,1433;
InitialCatalog=XXX;UserID=XXX@XXX;Password=XXX;MultipleActiveResultSets=True;
App=EntityFramework&quot;"providerName="System.Data.EntityClient" />
</connectionStrings>

它们似乎正确地链接到网站,因为web.config中的连接字符串与Azure中的连接字符串正确匹配,如in this answer所述,并且网站上的大多数视图都可正常工作.

但是,在尝试打开其中两个视图时,会出现以下错误:

Server Error in ‘/’ Application.

A network-related or instance-specific error occurred while establishing a connection to
SQL Server. The server was not found or was not accessible. Verify that the instance name
is correct and that SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)

Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated
in the code.

SQLExpress database file auto-creation error:

The connection string specifies a local Sql Server Express instance using a database
location within the application’s App_Data directory.

The provider attempted to automatically create the application services database because
the provider determined that the database does not exist. The following configuration
requirements are necessary to successfully check for existence of the application
services database and automatically create the application services database:

If the application is running on either Windows 7 or Windows Server 2008R2, special
configuration steps are necessary to enable automatic creation of the provider database.
Additional information is available at: 07001.
If the application’s App_Data directory does not already exist, the web server account must
have read and write access to the application’s directory. This is necessary because the web
server account will automatically create the App_Data directory if it does not already exist.

If the application’s App_Data directory already exists, the web server account only
requires read and write access to the application’s App_Data directory. This is necessary
because the web server account will attempt to verify that the Sql Server Express database
already exists within the application’s App_Data directory. Revoking read access on the
App_Data directory from the web server account will prevent the provider from correctly
determining if the Sql Server Express database already exists. This will cause an error when
the provider attempts to create a duplicate of an already existing database. Write access is
required because the web server account’s credentials are used when creating the new database.

Sql Server Express must be installed on the machine.
The process identity for the web
server account must have a local user profile. See the readme document for details on
how to create a local user profile for both machine and domain accounts.

似乎以下行可能在错误消息中最重要:

The connection string specifies a local Sql Server Express instance using a database location within the application’s App_Data directory.

两个不起作用的视图都是实例化AccountController并在其中使用以下方法:

[Authorize]
public class AccountController : Controller
{
    // ...
    public string FindUserArea(string id)
    {
        int locationId = FindUserLocationId(id);                
        Location location = db.Locations.FirstOrDefault(p => p.LocationID == locationId);

        return (string) (location.Suburb.SuburbName + " > "
            + location.Suburb.Region.RegionName + " > "
            + location.Suburb.Region.Province.ProvinceName);
    }
    // ...
}

我们首先怀疑AccountController中存在错误,但另一个也使用上述方法的视图显示正常.然后我们认为可能仍然存在与InitializeSimpleMembership相关的错误的可能性,因为这出现在几个问题中(我们不是100%确定SimpleMembership的含义/效果).我们在this question中遵循了主导,但是根据建议in this article没有解决错误成功添加InitializeSimpleMembership.

此外,在注册两个用户之一时会出现相同的错误,但是AspNetUser Azure DB仍然会更新,并且一旦刷新了新用户的站点登录就可以了.

最佳答案 这看起来很奇怪,当我从实体框架上下文和标准连接的混合中建立SQL连接时,我看到了其他奇怪的行为,但是如果你只有一个单独的数据库连接字符串可能不是问题.

我遇到的其他东西,但主要是由于死锁和超时问题,EF DbExecutionStrategy可以根据各种错误重新尝试调用,包括瞬态连接问题,这可能是尝试,请看这些链接:

http://www.codeproject.com/Tips/758469/Implementing-Connection-Resiliency-with-Entity-Fra

http://thedatafarm.com/data-access/ef6-connection-resiliency-for-sql-azure-when-does-it-actually-do-its-thing/

点赞