How to implement generic GetById() where Id can be of various types

前端 未结 3 1436
眼角桃花
眼角桃花 2020-12-08 22:55

I am trying to implement a generic GetById(T id) method which will cater for types which may have differing ID types. In my example, I have an entity which has

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 23:32

    You should remove the constraint on TId from your Repository class

    public abstract class Repository : IRepository
    where TEntity : class, IEntity
    {
        public virtual TEntity GetById(TId id)
        {
            return context.Set().Find(id);
        }
    }
    

提交回复
热议问题