Class GenericRepository<TContext, TEntity>
Generic base class for a repository.
Inherited Members
Namespace:Intranet.Common
Assembly:Intranet.Common.dll
Syntax
public abstract class GenericRepository<TContext, TEntity> : DisposableBase, IGenericRepository<TEntity>, IDisposable where TContext : DbContext, new ()where TEntity : class
Type Parameters
Name | Description |
---|---|
TContext | The type of the database context used by the repository. |
TEntity | The type of the entities in the repository. |
Constructors
| Improve this Doc View SourceGenericRepository(IDatabaseFactory<TContext>, ILoggerFactory)
Initialize a new instance of the GenericRepository<TContext, TEntity> class.
Declaration
protected GenericRepository(IDatabaseFactory<TContext> databaseFactory, ILoggerFactory loggerFactory)
Parameters
Type | Name | Description |
---|---|---|
IDatabaseFactory<TContext> | databaseFactory | |
ILoggerFactory | loggerFactory |
Properties
| Improve this Doc View SourceContext
Gets the database context.
Declaration
protected virtual TContext Context { get; }
Property Value
Type | Description |
---|---|
TContext | The database context. |
DatabaseFactory
Gets the database factory used by the repository.
Declaration
protected virtual IDatabaseFactory<TContext> DatabaseFactory { get; }
Property Value
Type | Description |
---|---|
IDatabaseFactory<TContext> | The database factory used by the repository. |
DbCommit
Gets or set a IDbCommit<TContext>.
Declaration
public IDbCommit<TContext> DbCommit { get; set; }
Property Value
Type | Description |
---|---|
IDbCommit<TContext> |
Methods
| Improve this Doc View SourceAdd(TEntity)
Adds the given entity to the repository.
Declaration
public virtual TEntity Add(TEntity entity)
Parameters
Type | Name | Description |
---|---|---|
TEntity | entity | The entity to add. |
Returns
Type | Description |
---|---|
TEntity | Returns the added entity. |
Implements
| Improve this Doc View SourceAddRange(IEnumerable<TEntity>)
Adds the given collection of entities into context underlying the set with each entity being put into the Added state such that it will be inserted into the database when SaveChangesAsync is called.
Declaration
public virtual IEnumerable<TEntity> AddRange(IEnumerable<TEntity> entities)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<TEntity> | entities | The collection of entities to add. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TEntity> | The collection of entities. |
Implements
| Improve this Doc View SourceAttach(TEntity)
Attaches the given entity to the current database context.
Declaration
public virtual TEntity Attach(TEntity entity)
Parameters
Type | Name | Description |
---|---|---|
TEntity | entity | The entity to attach. |
Returns
Type | Description |
---|---|
TEntity | Returns the attached entity. |
Implements
| Improve this Doc View SourceCountRecords()
Gets the number of records of the repositories table.
Declaration
public virtual int CountRecords()
Returns
Type | Description |
---|---|
System.Int32 | Returns the number records of the repositories table. |
Implements
| Improve this Doc View SourceCountRecordsAsync()
Gets the number of records of the repositories table asynchronously.
Declaration
public virtual Task<int> CountRecordsAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Int32> | Returns the number records of the repositories table. |
Implements
| Improve this Doc View SourceDisposed()
Dispose all managed resources.
Declaration
protected override void Disposed()
Overrides
| Improve this Doc View SourceEntry<TAnyEntity>(TAnyEntity)
Gets a System.Data.Entity.Infrastructure.DbEntityEntry<TEntity> Object for the given entity providing access to information about the entity and the ability to perform actions on the entity.
Declaration
public virtual DbEntityEntry<TAnyEntity> Entry<TAnyEntity>(TAnyEntity entity)where TAnyEntity : class
Parameters
Type | Name | Description |
---|---|---|
TAnyEntity | entity | The entity. |
Returns
Type | Description |
---|---|
System.Data.Entity.Infrastructure.DbEntityEntry<TAnyEntity> | Returns an entry for the entity. |
Type Parameters
Name | Description |
---|---|
TAnyEntity | The type of the entity. |
Implements
| Improve this Doc View SourceFindAsync(Object[])
Asynchronously finds an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.
Declaration
public virtual Task<TEntity> FindAsync(params object[] keyValues)
Parameters
Type | Name | Description |
---|---|---|
System.Object[] | keyValues | The values of the primary key for the entity to be found. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<TEntity> | A task that represents the asynchronous find operation. The task result contains the entity found, or null. |
Implements
Remarks
The ordering of composite key values is as defined in the EDM, which is in turn as defined in the designer, by the Code First fluent API, or by the DataMember attribute. Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.
GetAll()
Gets all entities.
Declaration
public virtual IQueryable<TEntity> GetAll()
Returns
Type | Description |
---|---|
System.Linq.IQueryable<TEntity> | All entities |
Implements
| Improve this Doc View SourceRemove(TEntity)
Removes the given entity from the repository.
Declaration
public virtual TEntity Remove(TEntity entity)
Parameters
Type | Name | Description |
---|---|---|
TEntity | entity | The entity to remove. |
Returns
Type | Description |
---|---|
TEntity | Returns the removed entity. |
Implements
| Improve this Doc View SourceSaveChanges()
Saves all changes made in this context to the underlying database.
Declaration
public virtual int SaveChanges()
Returns
Type | Description |
---|---|
System.Int32 | The number of Objects written to the underlying database. |
Implements
| Improve this Doc View SourceSaveChangesAsync(Nullable<CancellationToken>)
Asynchronously saves all changes made in this context to the underlying database.
Declaration
public virtual Task<int> SaveChangesAsync(CancellationToken? cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
System.Nullable<System.Threading.CancellationToken> | cancellationToken | A System.Threading.CancellationToken to observe while waiting for the task to complete. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Int32> | A task that represents the asynchronous save operation. The task result contains the number of Objects written to the underlying database. |
Implements
| Improve this Doc View SourceSetModified(TEntity)
Sets the state of the given entity to modified.
Declaration
public virtual void SetModified(TEntity entity)
Parameters
Type | Name | Description |
---|---|---|
TEntity | entity | The entity to mark as modified. |
Implements
| Improve this Doc View SourceSqlQuery(String, Object[])
Creates a raw SQL query that will return entities in this set. By default, the entities returned are tracked by the context; this can be changed by calling AsNoTracking on the System.Data.Entity.Infrastructure.DbSqlQuery<TEntity> returned. Note that the entities returned are always of the type for this set and never of a derived type. If the table or tables queried may contain data for other entity types, then the SQL query must be written appropriately to ensure that only entities of the correct type are returned. As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query String and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. context.Blogs.SqlQuery("SELECT * FROM dbo.Posts WHERE Author = @p0", userSuppliedAuthor); Alternatively, you can also construct a DbParameter and supply it to SqlQuery. This allows you to use named parameters in the SQL query string . context.Blogs.SqlQuery("SELECT * FROM dbo.Posts WHERE Author = @author", new SqlParameter("@author", userSuppliedAuthor));
Declaration
public virtual DbSqlQuery<TEntity> SqlQuery(string sql, params object[] parameters)
Parameters
Type | Name | Description |
---|---|---|
System.String | sql | The SQL query string . |
System.Object[] | parameters | The parameters to apply to the SQL query string . If output parameters are used, their values will not be available until the results have been read completely. This is due to the underlying behavior of DbDataReader, see http://go.microsoft.com/fwlink/?LinkID=398589 for more details. |
Returns
Type | Description |
---|---|
System.Data.Entity.Infrastructure.DbSqlQuery<TEntity> | A System.Data.Entity.Infrastructure.DbSqlQuery<TEntity> Object that will execute the query when it is enumerated. |
Implements
| Improve this Doc View SourceWhere(Expression<Func<TEntity, Boolean>>)
Gets the entities matching the given predicate.
Declaration
public virtual IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TEntity, System.Boolean>> | predicate | The predicate. |
Returns
Type | Description |
---|---|
System.Linq.IQueryable<TEntity> | The entities matching the given predicate. |