EF Core Generic Repository error with GetById method

0

I have a generic repository for my crud in netcore, but I have a problem with one of the methods.

The method is GetById

        public async Task<TEntity> GetById(int id)
    {
        return await _dbContext.Set<TEntity>()
                    .AsNoTracking()
                    .FirstOrDefault(t => t.Id == id);
    }

And in the% share% of%

There is an error

  

'TEntity' does not contain a definition for 'Id' nor is there any   Extension method 'Id' that accepts a first argument of type   'TEntity' (missing any using directive or a reference from   assembled?)

This would be the header of my GenericRepository

    public class GenericRepository<TEntity> : IGenericRepository<TEntity>
where TEntity : class
{
    private readonly DataContext _dbContext;

    public GenericRepository(DataContext dbContext)
    {
        _dbContext = dbContext;
    }
    
asked by PCH 07.02.2018 в 15:31
source

1 answer

0

This happens because in GetById method it receives a Generic type parameter, for that reason it does not contain an Id property, what you have to do is in your GetById method, replace your query with

return await _dbContext.Set<TEntity>().FindAsync(Id);
    
answered by 12.02.2018 в 16:49