Entity framework core Azure数据库在本地主机上工作,但与Azure服务应用程序一起使用时不工作

Entity framework core Azure数据库在本地主机上工作,但与Azure服务应用程序一起使用时不工作,entity-framework-core,azure-sql-database,azure-web-app-service,Entity Framework Core,Azure Sql Database,Azure Web App Service,所以我一直在尝试将我的第一个项目发布到azure。我已经准备好了一切,一个服务应用程序和一个sql数据库 我的初始页面加载正确(这是.net核心web应用程序的标准视图) 我需要做的第一件事是注册一个新用户。每当我尝试使用我的azure应用程序(myapp.azurewebsites.net)时,它都会失败,日志显示它与数据库相关 然而,我也尝试了同样的方法,在生产环境中的机器上运行应用程序,再次连接到azure sql server,一切都很完美。我可以注册用户,我可以创建帖子,我可以编辑他们

所以我一直在尝试将我的第一个项目发布到azure。我已经准备好了一切,一个服务应用程序和一个sql数据库

我的初始页面加载正确(这是.net核心web应用程序的标准视图)

我需要做的第一件事是注册一个新用户。每当我尝试使用我的azure应用程序(myapp.azurewebsites.net)时,它都会失败,日志显示它与数据库相关

然而,我也尝试了同样的方法,在生产环境中的机器上运行应用程序,再次连接到azure sql server,一切都很完美。我可以注册用户,我可以创建帖子,我可以编辑他们。
允许访问azure服务
选项已打开。此错误来自事件日志。我没有包括stacktrace

Category: Microsoft.EntityFrameworkCore.Query EventId: 10100 RequestId: 800001be-0000-ba00-b63f- 
84710c7967bb RequestPath: /Identity/Account/Register SpanId: |1e5a93ae-43f424904f38ea9f. TraceId: 
1e5a93ae-43f424904f38ea9f ParentId: ActionId: c3430236-e61c-4785-a3c3-4f60ba115b6e ActionName: 
/Account/Register An exception occurred while iterating over the results of a query for context type 
'MyApp.Data.ApplicationDbContext'. Microsoft.Data.SqlClient.SqlException (0x80131904): Server 
name cannot be determined. It must appear as the first segment of the server's dns name 
(servername.database.windows.net). Some libraries do not send the server name, in which case the 
server name must be included as part of the user name (username@servername). In addition, if both 
formats are used, the server names must match.
这些是我尝试将连接字符串添加到appsettings.json文件的不同方式。(已替换服务器名称、目录、用户和密码,它们已正确写入appsettings文件)


好吧,一天半之后,我终于修好了。解决方法很简单,很可能是我的新手犯的错误,造成了这么多麻烦

在那之后,我一直在学习如何设置应用程序和数据库连接的教程。在本教程中,正在使用的连接字符串是默认字符串,位于“myApp->Configuration->connection strings”中,格式为:

Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=@server.database.windows.net;Password=password;
这本书是在《指南》里写的,但不是为我写的。所以我所做的就是转到我的“sqldb->connectionstrings”并复制那里提供的一个。然后,我返回到应用程序配置,并使用SqlServer作为类型将其添加为新的配置字符串

此字符串的格式为:

Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

之后,应用程序开始正常工作。

目前,在本地运行是正常的。部署到Azure以显示与数据库相关的错误是否正确?建议您首先尝试登录门户并查找设置->配置->应用程序设置->连接字符串,以确保连接字符串中的名称与代码一致。@Jason我已经在azure设置中多次检查了连接字符串。有一次我甚至自己在appsettings中输入了它,以确保每个字符都是正确的。使用完全相同的凭据,我可以使用SSM登录azure db,并且在我自己启动应用程序时,我也可以向azure db发送请求。我不得不反过来!在Azure中创建Db为我生成了一个连接字符串,如“Server=tcp:servername”,但当我更改为“Data Source=azureDbUrl.database.windows.net”时,它开始工作。谢谢你的指点!对我来说,有或没有端口1433都不重要,两者都可以
Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=@server.database.windows.net;Password=password;
Data Source=tcp:server.database.windows.net,1433;
Initial Catalog=db;User Id=@server.database.windows.net;Password=password;
Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;