简体   繁体   中英

Last inserted id from generic repository

My Generic repository class

 public class Repository<TEntity, TId> : IRepository<TEntity, TId> where TEntity : class, IEntity<TId>
    {       
        protected readonly CBSContext _context;
        //private DbSet<TEntity> _entities;
      
        public Repository(CBSContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
      //      _entities = _context.Set<TEntity>();
        }
    
         public async Task<TEntity> AddAsync(TEntity entity)
            {
               await Task.Run(() => _context.Add(entity));         
               return entity;
            }

}

Am using ef core 5 Rc.. After insert id is not returning ? Any thing else need to be done..Thanks

EDIT:

After code change to not working..

   public async Task<TEntity> AddAsync(TEntity entity)
            {
               await _context.AddAsync(entity);         
               return entity;
            }

You need to use _context.SaveChangesAsync() after Add(). Then you can get id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM