T Entity不包含id的定义

问题描述 投票:0回答:1

我正在关注this教程,以便了解更好的通用存储库。

GetById方法:

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

但我得到这个错误:TEntity不包含id的定义。

这是我缺少的东西还是我做错了什么?

PS:我使用的是ASP.NET Core 2

asp.net-core-2.0 ef-core-2.0
1个回答
2
投票

本教程对泛型参数有类型约束

public interface IGenericRepository<TEntity>
    where TEntity : class, IEntity {
    //...
}

这假定IEntity接口具有Id属性,该属性未在文章中显示,但存在于文章中链接的源代码中。

public interface IEntity {
    int Id { get; set; }
}

这意味着与通用存储库一起使用的所有实体都需要从该接口派生才能使其工作。

© www.soinside.com 2019 - 2024. All rights reserved.