Use DbContext in other .NET Class Library

.net-core asp.net-core entity-framework-core

Question

I am using ASP.NET Core with EF Core Code First. I am defining the dependency injection of the DbContext in service configuration of the Startup.cs of my ASP.NET Core project like described here (https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html):

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<TestContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DevelopmentDatabase")));
    }

But I do not want to use the DbContext in a controller like described at the same site but I want to outsource it to another .NET Core Class Library project together with the Migration.

How do I accomplish this?

1
0
8/22/2016 2:39:08 PM

Popular Answer

Implement the Repository Pattern

It's explained clearly here: Building Your First Web API with ASP.NET Core MVC

But register your repository as:

services.AddScoped<ICustomerRepository, CustomerRepository>();

instead of using .AddSingleton as the link suggests, to avoid this other problem.

1
5/23/2017 12:33:54 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow