How can I return the ID of the Inserted Record in Entity Framework Generic Insert Method?

Viewed 2843

Here is the generic insert method. I need your suggestion to return the ID of the inserted record.

public static void Create<T>(T entity) where T : class
{
    using (var context = new InformasoftEntities())
    {
        DbSet dbSet = context.Set<T>();
        dbSet.Add(entity);
        context.SaveChanges();
    }
}
3 Answers
public async Task<int> Add(TEntity entity)
{
 await _context.Set<TEntity>().AddAsync(entity);
 await Save();
 return Task.FromResult(entity).Id;
}
Related