So I am using the repository pattern with asp.net core and entity framework 6 to access database from a service layer. The issue is that I have an entity that has a composite key and the generic repository takes an int as parameter so I can't pass multiple variables to find().

I tried changing int to object and sending object with 2 ids when I need to but that doesn't seem to work.

public interface IRepository<T> where T : class 
{
    IEnumerable<T> GetAll();
    T GetById( int id);
    void Update(T entity);
    void Create(T entity);
    void Delete(T entity);
}

1 Answers

1
Ahmed Yousif On Best Solutions

you can make it same signature of find method

public virtual TEntity Find(params object[] keyValues);

which accept key values as params of type object

so it will be

T GetById(params object[] keyValues);

so in this case it will accept any count of primary keys of any type

e.g.

new customerRepo().GetById(123)
new customerRepo().GetById(123,1)

and so on