SaveChangesOnDbContextAsync从未被调用。

huangapple go评论62阅读模式
英文:

SaveChangesOnDbContextAsync has never been called

问题

ABP已添加了一个可重写的函数,名为SaveChangesOnDbContextAsync,允许我们拦截任何插入/更新/删除操作,具体信息请参见此链接:https://github.com/abpframework/abp/issues/4659

我尝试重写SaveChangesOnDbContextAsync,在一个继承自AbpDbContext的类中,就像以下示例一样,但无论我何时更新数据,它都从未被调用:

public override async Task<int> SaveChangesOnDbContextAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
   var result = await base.SaveChangesAsync();
   return result;
}
英文:

ABP has added an overridable function called SaveChangesOnDbContextAsync that allow us to intercept any insert/update/delete operation as in this link: https://github.com/abpframework/abp/issues/4659

I tried to override SaveChangesOnDbContextAsync in a class that inherits AbpDbContext like the following but it has never been called whenever I update the data:

public override async Task&lt;int&gt; SaveChangesOnDbContextAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
   {
       var result = await base.SaveChangesAsync();
       return result;
   }

答案1

得分: 0

这个方法是为了让你的应用程序代码绕过AbpDbContext.SaveChangesAsync()中的逻辑而提供的。ABP不会调用它。

这是请求者的意思:

public override async Task&lt;int&gt; SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
    try
    {
        return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
    }
    catch (AbpDbConcurrencyException)
    {
        // 处理它
        // ...

        // 直接调用Microsoft.EntityFrameworkCore.DbContext的SaveChangesAsync(bool, CancellationToken),以避免反复创建审计日志、更新并发标记或触发实体事件等,就像AbpDbContext的版本所做的那样
        return await base.SaveChangesOnDbContextAsync(acceptAllChangesOnSuccess, cancellationToken);
    }
}

你可能只需要重写SaveChangesAsync

英文:

That method is provided for your application code to bypass the logic in AbpDbContext.SaveChangesAsync(). ABP does not call it.

This is what the requestor meant:

public override async Task&lt;int&gt; SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
    try
    {
        return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
    }
    catch (AbpDbConcurrencyException)
    {
        // Handle it
        // ...

        // Directly call Microsoft.EntityFrameworkCore.DbContext&#39;s SaveChangesAsync(bool, CancellationToken) to not repeatedly
        // create audit logs, update concurrencystamps, or trigger entity events etc. as AbpDbContext&#39;s version would do
        return await base.SaveChangesOnDbContextAsync(acceptAllChangesOnSuccess, cancellationToken);
    }
}

You probably just want to override SaveChangesAsync instead.

huangapple
  • 本文由 发表于 2023年2月8日 19:18:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定