Show / Hide Table of Contents

Class GenericRepository<TContext, TEntity>

Generic base class for a repository.

Inheritance
System.Object
LoggingBase
DisposableBase
GenericRepository<TContext, TEntity>
Inherited Members
DisposableBase.Dispose()
LoggingBase.Logger
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
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 Source

GenericRepository(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

A IDatabaseFactory<TContext>.

ILoggerFactory loggerFactory

A ILoggerFactory.

Properties

| Improve this Doc View Source

Context

Gets the database context.

Declaration
protected virtual TContext Context { get; }
Property Value
Type Description
TContext

The database context.

| Improve this Doc View Source

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.

| Improve this Doc View Source

DbCommit

Gets or set a IDbCommit<TContext>.

Declaration
public IDbCommit<TContext> DbCommit { get; set; }
Property Value
Type Description
IDbCommit<TContext>

A IDbCommit<TContext>.

Methods

| Improve this Doc View Source

Add(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
IGenericRepository<TEntity>.Add(TEntity)
| Improve this Doc View Source

AddRange(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
IGenericRepository<TEntity>.AddRange(IEnumerable<TEntity>)
| Improve this Doc View Source

Attach(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
IGenericRepository<TEntity>.Attach(TEntity)
| Improve this Doc View Source

CountRecords()

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
IGenericRepository<TEntity>.CountRecords()
| Improve this Doc View Source

CountRecordsAsync()

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
IGenericRepository<TEntity>.CountRecordsAsync()
| Improve this Doc View Source

Disposed()

Dispose all managed resources.

Declaration
protected override void Disposed()
Overrides
DisposableBase.Disposed()
| Improve this Doc View Source

Entry<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
IGenericRepository<TEntity>.Entry<TAnyEntity>(TAnyEntity)
| Improve this Doc View Source

FindAsync(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
IGenericRepository<TEntity>.FindAsync(Object[])
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.

| Improve this Doc View Source

GetAll()

Gets all entities.

Declaration
public virtual IQueryable<TEntity> GetAll()
Returns
Type Description
System.Linq.IQueryable<TEntity>

All entities

Implements
IGenericRepository<TEntity>.GetAll()
| Improve this Doc View Source

Remove(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
IGenericRepository<TEntity>.Remove(TEntity)
| Improve this Doc View Source

SaveChanges()

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
IGenericRepository<TEntity>.SaveChanges()
| Improve this Doc View Source

SaveChangesAsync(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
IGenericRepository<TEntity>.SaveChangesAsync(Nullable<CancellationToken>)
| Improve this Doc View Source

SetModified(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
IGenericRepository<TEntity>.SetModified(TEntity)
| Improve this Doc View Source

SqlQuery(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
IGenericRepository<TEntity>.SqlQuery(String, Object[])
| Improve this Doc View Source

Where(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.

Implements
IGenericRepository<TEntity>.Where(Expression<Func<TEntity, Boolean>>)
  • Improve this Doc
  • View Source
Back to top Copyright © 2015-2016 Microsoft
Generated by DocFX