【问题标题】:override SaveChangesAsync覆盖 SaveChangesAsync
【发布时间】:2015-05-29 01:16:14
【问题描述】:

我正在尝试在 MVC 项目上实施审计跟踪,方法是通过添加另一个功能来覆盖上下文(以便审计)。 SaveChanges 的覆盖工作正常,但是我遇到的问题是 SaveChangesAsync。 这是上下文中的部分代码

    public override Task<int> SaveChangesAsync()
    {
        throw new InvalidOperationException("User ID must be provided");
    }


    public override int SaveChanges()
    {
        throw new InvalidOperationException("User ID must be provided");
    }


    public async Task<int> SaveChangesAsync(int userId)
    {
        DecidSaveChanges(userId);
        return await this.SaveChangesAsync(CancellationToken.None);
    }


    public  int SaveChanges(int userId)
    {
        DecidSaveChanges(userId);
       return base.SaveChanges();
    }

我的控制器有问题

    await db.SaveChangesAsync(1);

1 是一个虚拟用户。我收到以下错误。

 Error  1   The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<System.Web.Mvc.ActionResult>'.   

你知道我在这里做错了吗?以及如何解决它?我正在使用 EF6 和 MVC5

【问题讨论】:

    标签: entity-framework model-view-controller


    【解决方案1】:

    你知道我在这里做错了什么吗?

    是的,看看你的编译器错误信息:

    The 'await' operator can only be used within an async method.
    

    因此,控制器操作(包含对SaveChangesAsync(1) 的调用)需要为async

    以及如何解决?

    是的,看看你的编译器错误信息:

    Consider marking this method with the 'async' modifier and changing its return type to 'Task<System.Web.Mvc.ActionResult>'.
    

    因此,您可以通过创建控制器操作 async 并将其返回类型从 ActionResult 更改为 Task&lt;ActionResult&gt; 来修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 2012-01-28
      • 2012-02-16
      • 1970-01-01
      相关资源
      最近更新 更多