我有一个同步方法,现在我想实现其异步版本。幸运的是,基础调用已经具有异步版本(dbSet.SaveChangesAsync()),但是在我的算法中,有一个if分支返回了常量常量1。

我不知道如何在异步版本中实现这一部分?

同步版本:

    public virtual int Add(T entity)
    {
        SetLogContext(entity, _logctx);
        dbSet.Add(entity);

        if (isAutonomous)
        {
            return ctx.SaveChanges();
        }
        return 1;
    }

异步版本:
    public virtual Task<int> AddAsync(T entity)
    {
        SetLogContext(entity, _logctx);
        dbSet.Add(entity);

        if (isAutonomous)
        {
            return ctx.SaveChangesAsync();
        }
        return ??? // What to write here?
    }

最佳答案


return Task.FromResult(1);

关于c# - 实现同步方法: How to return Task<int> which is constant 1?的异步版本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30366693/

10-13 00:01