().Remove(entity);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/CompanyEmployees/Repository/RepositoryManager.cs:
--------------------------------------------------------------------------------
1 | using Contracts;
2 | using Entities;
3 |
4 | namespace Repository
5 | {
6 | public class RepositoryManager : IRepositoryManager
7 | {
8 | private RepositoryContext _repositoryContext;
9 | private ExternalClientContext _externalClientContext;
10 |
11 | private ICompanyRepository _companyRepository;
12 | private IEmployeeRepository _employeeRepository;
13 |
14 | private IClientRepository _clientRepository;
15 |
16 | public RepositoryManager(RepositoryContext repositoryContext, ExternalClientContext externalClientContext)
17 | {
18 | _repositoryContext = repositoryContext;
19 | _externalClientContext = externalClientContext;
20 | }
21 |
22 | public ICompanyRepository Company
23 | {
24 | get
25 | {
26 | if (_companyRepository is null)
27 | _companyRepository = new CompanyRepository(_repositoryContext);
28 |
29 | return _companyRepository;
30 | }
31 | }
32 |
33 | public IEmployeeRepository Employee
34 | {
35 | get
36 | {
37 | if (_employeeRepository is null)
38 | _employeeRepository = new EmployeeRepository(_repositoryContext);
39 |
40 | return _employeeRepository;
41 | }
42 | }
43 |
44 | public IClientRepository Client
45 | {
46 | get
47 | {
48 | if (_clientRepository is null)
49 | _clientRepository = new ClientRepository(_externalClientContext);
50 |
51 | return _clientRepository;
52 | }
53 | }
54 |
55 | public void Save() => _repositoryContext.SaveChanges();
56 | }
57 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Code Maze
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Using Multiple Databases in ASP.NET Core via Entity Framework Core
2 | ## https://code-maze.com/aspnetcore-multiple-databases-efcore
3 | In this article, we are going to learn how to add multiple databases in the ASP.NET Core project using Entity Framework Core.
4 | We are going to show you how our repository pattern implementation helps us in the process by using abstractions that will hide all the implementation details from the presentation layer.
5 | Since we already have the repository pattern explained in our ASP.NET Core Repository Pattern article, we strongly suggest reading it first to learn more about the repository pattern implementation in ASP.NET Core Web API. We will modify the source code from that project and show you the benefits of the implementation of that pattern when adding multiple databases in an ASP.NET Core project.
6 | We are going to divide this article into the following sections:
7 |
8 | - Using Multiple Databases to Support Migrations
9 | - Registering Contexts for Multiple Databases in ASP.NET Core
10 | - Injecting Multiple Databases in the Repository Pattern
11 | - Testing Data Fetching From Multiple Databases in ASP.NET Core
12 | - Conclusion
13 |
14 |
--------------------------------------------------------------------------------