Getting Started
A library for implementing generic repositories and unit of work with Entity Framework Core. This implementation uses a single instance of the DbContext for all repositories to avoid concurrency issues.
Table of Contents
Installation
Using the NuGet package manager console within Visual Studio run the following command:
Install-Package Ortix.RepositoryPattern.EntityFrameworkCore
Or using the .NET Core CLI from a terminal window:
dotnet add package Qrtix.RepositoryPattern.EntityFrameworkCore
Creating the DbContext
Create your DbContext inheriting from Microsoft.EntityFrameworkCore.DbContext
:
public class MyDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
base.OnModelCreating(modelBuilder);
}
}
Configure your DbContext in the Program
class:
builder.Services.AddDbContext<DbContext, SuinQShopModuleDbContext>(opt =>
{
opt.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString"));
});
Injecting the RepositoryPattern's Services
Add the RepositoryPattern
services to the Service Collection:
builder.Services.AddRepositoryPattern(options => {
options.UseEntityFrameworkCore();
});
The default scope for injected services is scoped. If you want to change it, refer to the next examples:
Using the same lifetime:
builder.Services.AddRepositoryPattern(options => {
options.UseEntityFrameworCore(ServiceLifetime.Singleton);
});
Using individual lifetime:
builder.Services.AddRepositoryPattern(options => {
options.UseEntityFrameworCore(ServiceLifetime.Scoped, SeriviceLifetime.Singleton);
});