首页 > 解决方案 > 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.

标签: c#asp.netentity-framework-6aspnetboilerplate

解决方案


InsertAndGetIdAsync您明确需要Id.

var entityId = await _repository.InsertAndGetIdAsync(entity);

推荐阅读