Skip to content

Using DbRepositoryKey

StrutTower edited this page Jul 4, 2024 · 7 revisions

DbRepositoryKey is a new class that extends DbRepository. It is used for repositories that map to a table with a single primary key and provides extra built-in methods around the primary key.

Initialization is very similar to DbRepository except you have to provide the type for the primary key. The example shows this as int

public class PersonRepository : DbRepositoryKey<Person, int> {
    
    public PersonRepository(UnitOfWork uow) : base(uow.DbRepository) { }
} 

The primary key will be detected from the database mappings. If you have multiple primary keys it will throw an exception.

The following methods are provided by DbRepositoryKey:

  • GetByID() and GetByIDAsync()
    • Creates a query to pull an entity by the primary key. It will work with your primary key even if it's not called ID. I just used ID because it's more concise.
  • GetByIDs() and GetByIDsAsync()
    • Similar to GetByID but you can supplied an array of IDs and it will build a IN statement query to pull the entities
  • GetAllDictionary() and GetAllDictionaryAsync()
    • Return all entities in a dictionary with the primary key as the key of the dictionary.
Clone this wiki locally