【问题标题】:Use Azure Database (ASP.NET MVC)使用 Azure 数据库 (ASP.NET MVC)
【发布时间】:2025-11-21 13:10:01
【问题描述】:

我是 ASP.NET 开发的新手

我创建了 MVC 项目,据我所知,它使用默认的 .mdf 数据库创建。

我使用它,一切都很好,但我考虑将应用程序部署到 azure。

所以我需要使用 azure 数据库。

我创建它,一切正常。

我阅读了如何连接到 azure db 的文章。

据我了解,我需要连接字符串

在这里

Server=tcp:smartsolutionsserver.database.windows.net,1433;Initial Catalog=smartdatabase;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

据我了解,我需要将其粘贴到 Web.Release.config

我这样做了,所以现在看起来像这样

 <connectionStrings>
  <add name="DefaultConnection"
    connectionString="Server=tcp:smartsolutionsserver.database.windows.net,1433;Initial Catalog=smartdatabase;Persist Security Info=False;User ID=*********;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

但据我了解,.mdf 数据库连接写在 Web.config 文件中

看起来像这样

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-SmartSolutions-20170124034333.mdf;Initial Catalog=aspnet-SmartSolutions-20170124034333;Integrated Security=True" providerName="System.Data.SqlClient" />

告诉我,如何连接到 azure 数据库并使用它而不是本地 .mdf 数据库?

非常感谢您的帮助。

更新

我尝试添加 2 个新表并发布应用程序。从 SQL Server 对象资源管理器连接到数据库后,我看到添加了表。

但是当我尝试注册新用户时,我看到了这个

在本地一切正常

Screen

【问题讨论】:

  • 将特定的错误信息作为文本放入您的帖子中。 Imgur 已关闭。
  • 您是否将 Azure SQL Server 上 Web 应用的 IP 地址列入白名单?
  • 您需要记录发生的错误或在 Web.config 中关闭自定义错误才能看到它。否则我们只能猜测。

标签: c# asp.net asp.net-mvc asp.net-mvc-4 azure


【解决方案1】:

所以我的错误在The model backing the 'ApplicationDbContext' context has changed since the database was created

我去表__MigrationHistory 并删除行。在那之后,一切都好。所有作品

@ juunas 谢谢你的建议。我这样做并看到错误。如果您使用 IIS 控制台,它也可以看到

【讨论】: