I want to retrieve multiple records by giving array of primary key and I have to make generic method of it for all the entities.
private DbSet<TEntity> _entities;
/// <summary>
/// Get entity by identifier
/// </summary>
/// <param name="id">Identifier</param>
/// <returns>Entity</returns>
public virtual TEntity GetById(object id)
{
return Entities.Find(id);
}
/// <summary>
/// Get entity by identifier
/// </summary>
/// <param name="id">Identifier</param>
/// <returns>Entity</returns>
public virtual List<TEntity> GetByIds(int id[])
{
// want to make it generic
return Entities.Where(x=>id.Contains(id));
}
/// <summary>
/// Gets an entity set
/// </summary>
protected virtual DbSet<TEntity> Entities
{
get
{
if (_entities == null)
_entities = _context.Set<TEntity>();
return _entities;
}
}
problem here is that my Entities doesn't have ID columns, for eg Product has ProductId, Order has OrderId. I don't want to change my db columns to Id.
Entities.Where(x=>id.Contains(id));
I want my entities columns to be same as they are now. can I achieve a generic search method with this db structure to find multiple records?
See Question&Answers more detail:os