首页 > 解决方案 > 插入后是否可以刷新数据?

问题描述

我使用 EF / AutoMapping。我想在插入后立即记录的 ID(PK)。你是怎样做的?插入后是否可以立即刷新数据?

public int InsertArtist(ArtistDto artistDto)
{
    Artist entity = _mapper.Map<ArtistDto, Artist>(artistDto);

    _artistRepository.Insert(entity);
    _artistRepository.Save();

    return entity.Id;
}

标签: c#entity-frameworkautomapping

解决方案


默认情况下它正在刷新数据,假设您的 Id 是数据库生成的,那么在保存实体后,实体框架将分配和 id 给该实体并返回引用的实体。


推荐阅读