c# - repository.InsertAsync always returns entity.Id 0
问题描述
Sometimes I need Id
of inserted entity. To achieve this, I use:
entity = await _repository.InsertAsync(entity);
With this, entity.Id
is always 0. Because of this, I'm adding the line below.
await CurrentUnitOfWork.SaveChangesAsync();
If I add this line, I can get the Id
. But, this way seems dirty to me. Is there another way? Or is this way already correct? I just want to be sure. I want to know that using CurrentUnitOfWork
will not be cause problems that I may not know. Thank you.
解决方案
当InsertAndGetIdAsync
您明确需要Id
.
var entityId = await _repository.InsertAndGetIdAsync(entity);
推荐阅读
- html - tailwindcss 中的导航栏转换
- python - BeautifulSoup - 从 HTML 中去除不间断的空格
- java - 如何使用我的比较器对 TreeSet 中的重复项进行排序和避免重复?
- list - 如何在方案中编写程序来查找数字列表的因子
- java - Java AffineTransform 在不同系统上的工作方式不同
- javascript - HTML嵌入链接中对应于100vh的动态iframe?
- python - 如何在python中自动化图像截图过程?
- python - python ctypes中int32_t和char数组的复杂结构
- google-cloud-platform - 谷歌云存储 | Artifactory 不删除存储桶中的对象
- reactjs - 在 React 中查找文件