2

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?

1 Answer 1

7

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("..."))
Sign up to request clarification or add additional context in comments.

2 Comments

Ok this is a helpful hint, maybe you could elaborate a little more on how to treat this kind of change in the ProjectsController and above all in the ProjectRepository class.
and the migrations? How to get the migrations of the different contexts with this response?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.