db_owner 权限对使用 Entity Framework Core 的 ASP.NET Core 8 Web API 中的分布式事务的影响

问题描述 投票:0回答:1

我正在使用 ASP.NET Core 8 Web API,该 API 跨托管在单独的 Azure SQL 服务器上的两个不同数据库执行插入/更新/删除操作。该 API 使用存储库级方法来处理这些操作,有效地遵循分布式事务模式。

Azure SQL 数据库的身份验证是使用用户分配的托管标识进行管理的,该标识具有以下权限:

db_datareader, db_datawriter, db_executor

运行 API 端点时,出现以下错误:

Microsoft.EntityFrameworkCore.DbUpdateException:

at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+<ExecuteAsync>d__50.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch+<ExecuteAsync>d__15.MoveNext (Microsoft.EntityFrameworkCore.SqlServer, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor+<ExecuteAsync>d__9.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor+<ExecuteAsync>d__9.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor+<ExecuteAsync>d__9.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager+<SaveChangesAsync>d__111.MoveNext (Microsoft.EntityFrameworkCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager+<SaveChangesAsync>d__115.MoveNext (Microsoft.EntityFrameworkCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy+<ExecuteAsync>d__30`2.MoveNext (Microsoft.EntityFrameworkCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.DbContext+<SaveChangesAsync>d__63.MoveNext (Microsoft.EntityFrameworkCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Microsoft.EntityFrameworkCore.DbContext+<SaveChangesAsync>d__63.MoveNext (Microsoft.EntityFrameworkCore, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)  
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)  
at Shared.Infrastructure.Persistence.Context.ModuleDbContext+<SaveChangesAsync>d__5.MoveNext (Shared.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\a\1\s\DemoDbContext.cs:53)

这是来自

DemoDbContext.cs
的片段:

public override async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
    return await base.SaveChangesAsync(true, cancellationToken);
}

在此设置中,有两个独立的

DbContext
文件继承自
DemoDbContext
并使用
SaveChangesAsync
方法进行保存操作。

授予

db_owner
权限对整个方法实现有什么影响,特别是在分布式事务的上下文中?我应该注意任何潜在的安全或性能影响吗?

任何人都可以帮我提供一些代码示例,作为我的实现的参考吗?

entity-framework-core azure-sql-database .net-8.0
1个回答
0
投票

Microsoft.EntityFrameworkCore.DbUpdateException:

当 Entity Framework Core (EF Core) 在尝试将更改保存到数据库时遇到问题时,通常会出现

Microsoft.EntityFrameworkCore.DbUpdateException
。这可能是由于各种原因造成的,例如违反约束、数据类型不匹配或数据库连接问题。

要解决此问题,如果您遇到以下问题,则需要检查以下问题:

  • 约束违规:检查是否违反了应用于表的任何约束,例如 NOT NULL、UNIQUE、PRIMARY KEY、FOREIGN KEY、CHECK、DEFAULT、CREATE INDEX。
  • 数据类型不匹配:验证保存的数据是否与数据库架构匹配(例如,字符串长度、可为空的列等)。
  • 数据库连接:检查您尝试连接数据库的用户的权限。
© www.soinside.com 2019 - 2024. All rights reserved.