gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:15:08 26 4
gpt4 key购买 nike

我们有一个 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而且网站上的大多数 View 都运行良好。

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

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: http://go.microsoft.com/fwlink/?LinkId=160102. 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.

两个不起作用的 View 都实例化 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 中存在错误。 ,但另一个也使用上述方法的 View 显示良好。然后我们认为仍然有可能出现与 InitializeSimpleMembership 相关的错误。因为这出现在几个问题中(我们不能 100% 确定 SimpleMembership 的含义/效果)。我们跟随 this question 的领先,但成功添加 InitializeSimpleMembership根据建议in this article没有解决错误。

此外,两个用户之一在注册时也会出现相同的错误,但是 AspNetUser Azure DB 仍会更新,刷新后即可使用新用户登录站点。

最佳答案

这看起来确实很奇怪,当 SQL 连接是由 Entity Framework 上下文和标准连接混合创建时,我见过其他奇怪的行为,但是如果您只有 1 个数据库连接字符串,则可能不是问题。

我还遇到过其他问题,但主要是由于死锁和超时问题,EF DbExecutionStrategy 可以根据各种错误(包括 transient 连接问题)重试调用,这可能是值得尝试的事情,请参阅以下链接:

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/

关于asp.net-mvc - 由于基于角色的 [Authorize] 属性,ASP.NET MVC 5 Azure 网站无法在某些 View 上连接到 SQL Azure DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25604296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com