代码之家  ›  专栏  ›  技术社区  ›  Alexandra Damaschin

t实体不包含id的定义

  •  0
  • Alexandra Damaschin  · 技术社区  · 7 年前

    我在跟踪 this 教程,以便更好地理解通用存储库。

    GetByID方法:

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

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

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

    PS:我正在使用ASP.NET Core 2

    1 回复  |  直到 7 年前
        1
  •  2
  •   Nkosi    7 年前

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

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

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

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

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