Skip to content

Latest commit

 

History

History
83 lines (60 loc) · 2.47 KB

File metadata and controls

83 lines (60 loc) · 2.47 KB

Repository Pattern implementation for Entity Framework Core

NuGet Version NuGet Downloads GitHub Repo stars

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.

Consult the online documentation for more details.

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);
});