asynchronous - 如何检查是否在 Async C# MVC Core 方法中添加了实体项
问题描述
我正在编写自己的 IUserStore 和 IUserPasswordStore 实现,我需要准备CreateAsync
将返回的异步方法IdentityResult
。到目前为止,我有这个:
public async Task<IdentityResult> CreateAsync(User user)
{
Users.Add(user);
//await Users.AddAsync(user);
SaveChanges();
//await SaveChangesAsync(user);
//alternative version is commented
if (<What to put there>)
{
return IdentityResult.Success;
}
return IdentityResult.Failed(new IdentityError { Description = $"Could not insert user {user.Email}." });
}
我真的不知道我应该在里面放什么if
。我只能使用异步方法,所以当我运行这个函数时没有任何结果。有小费吗?我真的很感激。
通常我只是使用Any()
,但这个实现不允许我。如果您需要更多代码,请在评论中提问,我会提供,但我认为这是足够的信息。
解决方案
SaveChanges()
await SaveChangesAsync();
如果保存操作成功完成,则返回一个 int 表示受影响记录的数量。您可以检查返回值并返回相关结果。
var success = await context.SaveChangesAsync() > 0;
return success
? IdentityResult.Success
: IdentityResult.Failed;
推荐阅读
- javascript - 模块“domhandler”没有导出成员“DomElement”。您的意思是改用“从“domhandler”导入 DomElement”吗?
- python - 网格搜索熊猫数据框中的每一行
- postgresql - ThreadAbortException:系统错误将 Postgres 函数(表返回)的结果读取到 EF 可枚举中
- r - 对 R 中的连续值求和,按特定值分解
- php - 如何格式化 PostgreSQL 查询以干净地返回 geojson?
- vert.x - Vertx mysql客户端异步和同步
- mongodb-query - MongoDB - 解释与日志 - 检查的键不匹配
- reactjs - 使用 Apollo GraphQL 客户端进行 React 时出现意外输出
- html - 使用用户配置文件创建 html/js 帐户
- pandas - 从每个组中删除第一个 n