Skip to content

User guide

Emil H. Södergren edited this page Mar 9, 2018 · 2 revisions

Welcome to the EFCore.Repository wiki!

The purpose is to provide an abstraction to Entity Framework Core by the use of generic methods with overloads.

HOW TO (ASP.NET Core)

In the ConfigureServices method in the Startup class, EFCore.Repository provides extensions to IServiceCollection to inject DbContext and EFCore.Repository.IRepository with a single method.

Example 1

public void ConfigureServices(IServiceCollection services)
{
    services.AddRepositoryWithDbContext<AppDbContext>(builder =>
    {
        builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    });

    services.AddMvc();
}

Calling AddRepositoryWithDbContext<DbContext>(...) in Startup makes IRepository configured to use the supplied DbContext. Thus, IRepository can be used anywhere in the application.

Example 2

Assumed the DbContext has a DbSet of class Product, we can use it this way to return a model of List using IRepository.

    public class HomeController : Controller
    {
        private readonly IRepository _repository;

        public HomeController(IRepository repository)
        {
            _repository = repository;
        }

        public IActionResult Index()
        {
            var model = _repository.ToList<Product>();

            return View(model);
        }
    }

Clone this wiki locally