indexoutofbounds
indexoutofbounds

Reputation: 803

Using multiple databases in a single webapi Controller

I have a dotnet core (2.1) webapi project with EntityFramework (2.1)

What I have is a IProjectRepository interface:

public interface IProjectRepository
{
    void GetProject(Guid id);
    void DeleteProject(Guid id);
    Project CreateProject(Project prj);
    int SaveChanges();
}

I also implement it in the ProjectRepository class:

public class ProjectRepository : IProjectRepository
{
    private readonly ProjectContext context;

    public ProjectRepository(ProjectContext context)
    {
        this.context = context;
    }

    public Project GetProject(Guid id)
    {
        return context.Projects.Find( id );
    }

    public void DeleteProject(Guid id)
    {
        context.Projects.Remove( project );
    }

    public void CreateProject(Project prj)
    {
        context.Projects.Add(project);
    }

    public int SaveChanges()
    {
        return context.SaveChanges();
    }
}

Further I have a ProjectsController:

[Route( "api/[controller]" )]
[ApiController]
public class ProjectsController : ControllerBase
{
    private readonly IProjectRepository projectRepository;

    public ProjectsController(IProjectRepository projectRepository)
    {
        this.projectRepository = projectRepository;
    }

    [HttpGet( "{id}", Name = "GetProject" )]
    public ActionResult<Project> GetProjectById( Guid id )
    {
        Project project = projectRepository.GetProject( id );

        if ( project == null )
        {
            return NotFound();
        }
        return project;
    }

    [HttpDelete( "{id}" )]
    public IActionResult DeleteProject( Guid id )
    {
        Project project = projectRepository.GetProject( id );

        if ( project == null )
        {
            return NotFound();
        }

        projectRepository.RemoveProject( project );
        projectRepository.SaveChanges();
        return NoContent();
    }

    /* etc. */
}

I am configuring the repository in the Startup.cs - ConfigureServices function:

services.AddDbContext<ProjectContext>( opt => opt.UseSqlite( "Data Source=" + projectsPath + "\\Projects.db" ) );
services.AddScoped<IProjectRepository, ProjectRepository>();

What I want to achieve is to use different databases inside a single controller and also using an a single Model. So assuming I have two different HttpPost-Requests in my ProjectController:

[HttpPost("{id}/foo")]

and

[HttpPost("{id}/bar")]

In case of foo I want to write to foo.db and in case of bar I want to write to bar.db. Simply adding multiple calls to services.AddDbContext<ProjectContext> obviously will not work. What is a good approach in order to achive my goal?

Upvotes: 2

Views: 3011

Answers (1)

Shaun Luttin
Shaun Luttin

Reputation: 141792

Simply adding multiple calls to services.AddDbContext obviously will not work.

That approach would work if you used inheritance and called AddDbContext with the child classes.

Define two more contexts that inherit from ProjectContext.

public class FooContext : ProjectContext {}

public class BarContext : ProjectContext {}

Then register them both with different connection strings.

services.AddDbContext<FooContext>(opt => opt.UseSqlite("..."))

services.AddDbContext<BarContext>(opt => opt.UseSqlite("..."))

Upvotes: 7

Related Questions