Azure 错误 - 连接字符串困难

Azure error - connection string difficulties

我正在使用 vs 2015 中的 mvc5 应用程序。 每当我尝试访问数据库中的项目时,我都会收到错误消息

Login failed for user '{your_username}'.

当我使用 Azure 门户 "allow access to azure services" 设置时,我的客户端 IP 地址已列出。 我认为我的连接字符串配置不正确。我不知道如何在我的本地机器上正确配置它并使用 azure。

我将我的网络配置中的连接字符串保存到我以前工作的本地服务器:

在我的web.config

<add name="IdentityDbContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=C:\Users\UserName\EduSmart_db.Mdf;          Initial Catalog=EduSmart_db;Integrated Security=True" providerName="System.Data.SqlClient" />

在 Azure 门户连接字符串中

Server=tcp:xxxx.database.windows.net,1433;Initial Catalog=Edu;Persist Security Info=False;User ID=Name;Password=Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

How can I tell which connection string azure is using?

Azure 使用第二个连接字符串。第一个(localdb)刚刚在您的本地机器上使用。服务器无法获取它。您可以在 Context class 中添加 Azure Sql 连接字符串名称来指定它。

Is there a different problem i have not considered?

您可以通过以下方式查看:

1.The Azure Sql Database 中的防火墙设置。你可以定义一个 ip 地址范围(如 ×.×.×.0 到 ×.×.×.255)而不是只有一个 ip 地址。

2.You 可以检查 web.configs 文件中的 Azure sql 连接字符串,例如 这个。它在我这边很好用。

 <connectionStrings>
        <add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Data Source=tcp:[databasename].database.windows.net,1433;Initial Catalog=[databasename];Integrated Security=False;User Id=[username];Password=[password];Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" />   
   </connectionStrings>

如果您使用 Azure Sql 的 Code First,您可以在 Context class:

中添加相关的连接字符串名称
 public ApplicationDbContext(): base("ConnectionStringName")
 {
 }

3.You 可以单击 工具>连接到数据库>Microsoft SQL 服务器>输入您的服务器名称、用户名和有关 Azure 的密码 Sql。然后点击测试连接按钮检查是否可以连接成功。

此外,您可以参考此article以了解如何在您的项目中本地访问 Azure Sql。

您只需按照说明将 Web 应用程序和数据库发布到 Azure here

要发布您的网络应用程序,right-click 项目并 select 发布。在创建站点页面上提供现有 SQL Azure 服务器、数据库和登录凭据。

发布时会提供两个数据库连接,其中之一是DBContext。当您使用向导发布网站时,数据表不会发布。

由于您已经在 SQL Azure 上配置了防火墙设置,要发布数据库请转到数据库项目。 Right-click 项目和 select 发布。在“发布数据库”对话框中,单击“编辑并更新有关 Azure 数据库的信息”。保存配置文件并发布数据库。