我在尝试从 ASP.NET Core 应用程序运行实体框架 (EF) 更新命令 (
dotnet ef database update
) 时遇到错误。该应用程序连接到 Azure 上托管的数据库。执行 EF 更新命令后,我收到两个不同的错误:
An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' call.
A
DefaultAzureCredential
错误,指示尽管尝试执行故障排除步骤在 Visual Studio Code 中重新验证我的 Azure 帐户,但仍无法从包含的凭据中检索令牌。查看错误(This account '***' needs re-authentication. Please go to Tools->Options->Azure Services Authentication, and re-authenticate the account you want to use..
- Stored credentials not found. Need to authenticate user in VSCode Azure Account. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/vscodecredential/troubleshoot
- Azure CLI not installed
- PowerShell is not installed.)
我重新配置了我的 SQL 调用以使用 EnableRetryOnFailure 方法来启用瞬态错误弹性,希望能够解决此问题,但它没有修复它。然后按照为 DefaultAzureCredential 错误提供的故障排除指南,在 Visual Studio Code 中重新验证我的 Azure 帐户,不幸的是,这也没有对其进行排序。
我已经针对本地数据库对其进行了测试,但仍然遇到相同的错误!
此外,Azure CLI 已经安装,所以不太清楚为什么它说没有安装!
任何解决此问题的指导或帮助将不胜感激。谢谢。
实体框架中的暂时故障可能由于多种原因而发生,包括网络问题、数据库服务器暂时不可用、超时或资源争用。这些故障通常是间歇性和暂时性的,因此称为“暂时性”。 使用
EnableRetryOnFailure方法实现重试逻辑,以自动重试失败的数据库操作。 实施适当的错误处理以捕获瞬态故障异常并根据需要重试数据库操作。 监控和诊断网络和数据库服务器问题,以解决暂时性故障的根本原因。 根据提示在 Visual Studio Code 中重新验证您的 Azure 帐户。 清除所有缓存的凭据并确保为 Azure 服务授予必要的权限。
我按照 Visual Studio Code 中的以下步骤创建了一个带有 Azure 数据库的 ASP .NET Core 8 应用程序。
我已将所需的包添加到项目中,如下所述。
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration
:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="8.0.2" />
</ItemGroup>
</Project>
我将以下代码添加到我的 Program.cs 文件中。
builder.Services.AddDbContext<Studentdbcontext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("StudentConnection")));
这是我完整的Program.cs
:
using Microsoft.EntityFrameworkCore;
using MyMvcApp.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<Studentdbcontext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("StudentConnection")));
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
确保您的 ConnectionString 正确。appsettings.json
"AllowedHosts": "*",
"ConnectionStrings": {
"StudentConnection": "Server=tcp:<AzureServerName>.database.windows.net,1433;Initial Catalog=<AzureDataBAseName>;Persist Security Info=False;User ID=<UserName>;Password=<Password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
}}
下面是我的模型类和 DbContext 类。我使用以下命令生成控制器和视图代码:
dotnet aspnet-codegenerator controller -name <ControllerName> -m <ModelClassName> -dc <DbContextName> --relativeFolderPath Controllers --useDefaultLayout
:
namespace MyMvcApp.Models
{
public class Students
{
public int Id { get; set; }
public string? Name { get; set; }
}
}
:
using Microsoft.EntityFrameworkCore;
namespace MyMvcApp.Models
{
public class Studentdbcontext:DbContext
{
public Studentdbcontext(DbContextOptions<Studentdbcontext> options)
: base(options)
{
}
public DbSet<Students> students { get; set; }
}
}
我使用以下命令安装了dotnet-ef
:
dotnet tool install --global dotnet-ef
如下图。
在Azure Portal中创建SQL Server和数据库后,将您当前的IP地址添加到SQL Server中网络的防火墙规则中,如下所示。
我成功启用了迁移并更新了数据库,如下所示。
dotnet ef migrations add initalmigrations
dotnet ef database update
输出
: