├── .gitignore ├── CompanyEmployees ├── CompanyEmployees.sln ├── CompanyEmployees │ ├── CompanyEmployees.csproj │ ├── Controllers │ │ └── CompaniesController.cs │ ├── Extensions │ │ ├── ExceptionMiddlewareExtensions.cs │ │ └── ServiceExtensions.cs │ ├── MappingProfile.cs │ ├── Migrations │ │ ├── 20190927181200_DataBaseCreation.Designer.cs │ │ ├── 20190927181200_DataBaseCreation.cs │ │ ├── 20190927182016_InitialData.Designer.cs │ │ ├── 20190927182016_InitialData.cs │ │ ├── ExternalClient │ │ │ ├── 20211115113236_ExternalClientData.Designer.cs │ │ │ ├── 20211115113236_ExternalClientData.cs │ │ │ └── ExternalClientContextModelSnapshot.cs │ │ └── RepositoryContextModelSnapshot.cs │ ├── Program.cs │ ├── Properties │ │ └── launchSettings.json │ ├── Startup.cs │ ├── WeatherForecast.cs │ ├── appsettings.Development.json │ ├── appsettings.json │ └── nlog.config ├── Contracts │ ├── Contracts.csproj │ ├── IClientRepository.cs │ ├── ICompanyRepository.cs │ ├── IEmployeeRepository.cs │ ├── ILoggerManager.cs │ ├── IRepositoryBase.cs │ └── IRepositoryManager.cs ├── Entities │ ├── Configuration │ │ ├── ClientConfiguration.cs │ │ ├── CompanyConfiguration.cs │ │ └── EmployeeConfiguration.cs │ ├── DataTransferObjects │ │ └── CompanyDto.cs │ ├── Entities.csproj │ ├── ErrorModel │ │ └── ErrorDetails.cs │ ├── ExternalClientContext.cs │ ├── Models │ │ ├── Client.cs │ │ ├── Company.cs │ │ └── Employee.cs │ └── RepositoryContext.cs ├── LoggerService │ ├── LoggerManager.cs │ └── LoggerService.csproj └── Repository │ ├── ClientRepository.cs │ ├── CompanyRepository.cs │ ├── EmployeeRepository.cs │ ├── Repository.csproj │ ├── RepositoryBase.cs │ └── RepositoryManager.cs ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29318.209 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompanyEmployees", "CompanyEmployees\CompanyEmployees.csproj", "{8980D1CF-20AB-488F-A809-A297D21DAE9A}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contracts", "Contracts\Contracts.csproj", "{D573853A-C4FE-4F59-A6C2-5BD50D481551}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggerService", "LoggerService\LoggerService.csproj", "{CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{48986B5B-FFB7-44D7-9A7E-76BC389EA83B}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "Repository\Repository.csproj", "{80FA5BD0-F17B-4F58-925D-EC359B951EB8}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | GlobalSection(ExtensibilityGlobals) = postSolution 47 | SolutionGuid = {7E2E5905-0DED-4CBE-804E-1A7A08496593} 48 | EndGlobalSection 49 | EndGlobal 50 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/CompanyEmployees.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | all 12 | runtime; build; native; contentfiles; analyzers; buildtransitive 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Controllers/CompaniesController.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Contracts; 3 | using Entities.DataTransferObjects; 4 | using Microsoft.AspNetCore.Mvc; 5 | using System.Collections.Generic; 6 | 7 | namespace CompanyEmployees.Controllers 8 | { 9 | [Route("api/companies")] 10 | [ApiController] 11 | public class CompaniesController : ControllerBase 12 | { 13 | private readonly IRepositoryManager _repository; 14 | private readonly ILoggerManager _logger; 15 | private readonly IMapper _mapper; 16 | 17 | public CompaniesController(IRepositoryManager repository, ILoggerManager logger, IMapper mapper) 18 | { 19 | _repository = repository; 20 | _logger = logger; 21 | _mapper = mapper; 22 | } 23 | 24 | [HttpGet] 25 | public IActionResult GetCompanies() 26 | { 27 | var companies = _repository.Company.GetAllCompanies(trackChanges: false); 28 | 29 | var companiesDto = _mapper.Map>(companies); 30 | 31 | return Ok(companiesDto); 32 | } 33 | 34 | [HttpGet("clients")] 35 | public IActionResult GetTests() 36 | { 37 | var tests = _repository.Client.GetAllClients(trackChanges: false); 38 | 39 | return Ok(tests); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Extensions/ExceptionMiddlewareExtensions.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities.ErrorModel; 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Diagnostics; 5 | using Microsoft.AspNetCore.Http; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Net; 10 | using System.Threading.Tasks; 11 | 12 | namespace CompanyEmployees.Extensions 13 | { 14 | public static class ExceptionMiddlewareExtensions 15 | { 16 | public static void ConfigureExceptionHandler(this IApplicationBuilder app, ILoggerManager logger) 17 | { 18 | app.UseExceptionHandler(appError => 19 | { 20 | appError.Run(async context => 21 | { 22 | context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 23 | context.Response.ContentType = "application/json"; 24 | 25 | var contextFeature = context.Features.Get(); 26 | if (contextFeature != null) 27 | { 28 | logger.LogError($"Something went wrong: {contextFeature.Error}"); 29 | 30 | await context.Response.WriteAsync(new ErrorDetails() 31 | { 32 | StatusCode = context.Response.StatusCode, 33 | Message = "Internal Server Error." 34 | }.ToString()); 35 | } 36 | }); 37 | }); 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Extensions/ServiceExtensions.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using LoggerService; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.EntityFrameworkCore; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.DependencyInjection; 8 | using Repository; 9 | 10 | namespace CompanyEmployees.Extensions 11 | { 12 | public static class ServiceExtensions 13 | { 14 | public static void ConfigureCors(this IServiceCollection services) => 15 | services.AddCors(options => 16 | { 17 | options.AddPolicy("CorsPolicy", builder => 18 | builder.AllowAnyOrigin() 19 | .AllowAnyMethod() 20 | .AllowAnyHeader()); 21 | }); 22 | 23 | public static void ConfigureIISIntegration(this IServiceCollection services) => 24 | services.Configure(options => 25 | { 26 | 27 | }); 28 | 29 | public static void ConfigureLoggerService(this IServiceCollection services) => 30 | services.AddScoped(); 31 | 32 | public static void ConfigureSqlContext(this IServiceCollection services, IConfiguration configuration) => 33 | services.AddDbContext(opts => 34 | opts.UseSqlServer(configuration.GetConnectionString("sqlConnection"), b => b.MigrationsAssembly("CompanyEmployees"))); 35 | 36 | public static void ConfigureExternalClientContext(this IServiceCollection services, IConfiguration configuration) => 37 | services.AddDbContext(opts => 38 | opts.UseSqlServer(configuration.GetConnectionString("externalClientConnection"), b => b.MigrationsAssembly("CompanyEmployees"))); 39 | 40 | public static void ConfigureRepositoryManager(this IServiceCollection services) => 41 | services.AddScoped(); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/MappingProfile.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Entities.DataTransferObjects; 3 | using Entities.Models; 4 | 5 | namespace CompanyEmployees 6 | { 7 | public class MappingProfile : Profile 8 | { 9 | public MappingProfile() 10 | { 11 | CreateMap() 12 | .ForMember(c => c.FullAddress, 13 | opt => opt.MapFrom(x => string.Join(' ', x.Address, x.Country))); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/20190927181200_DataBaseCreation.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace CompanyEmployees.Migrations 11 | { 12 | [DbContext(typeof(RepositoryContext))] 13 | [Migration("20190927181200_DataBaseCreation")] 14 | partial class DataBaseCreation 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("Entities.Models.Company", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnName("CompanyId") 29 | .HasColumnType("uniqueidentifier"); 30 | 31 | b.Property("Address") 32 | .IsRequired() 33 | .HasColumnType("nvarchar(60)") 34 | .HasMaxLength(60); 35 | 36 | b.Property("Country") 37 | .HasColumnType("nvarchar(max)"); 38 | 39 | b.Property("Name") 40 | .IsRequired() 41 | .HasColumnType("nvarchar(60)") 42 | .HasMaxLength(60); 43 | 44 | b.HasKey("Id"); 45 | 46 | b.ToTable("Companies"); 47 | }); 48 | 49 | modelBuilder.Entity("Entities.Models.Employee", b => 50 | { 51 | b.Property("Id") 52 | .ValueGeneratedOnAdd() 53 | .HasColumnName("EmployeeId") 54 | .HasColumnType("uniqueidentifier"); 55 | 56 | b.Property("Age") 57 | .HasColumnType("int"); 58 | 59 | b.Property("CompanyId") 60 | .HasColumnType("uniqueidentifier"); 61 | 62 | b.Property("Name") 63 | .IsRequired() 64 | .HasColumnType("nvarchar(30)") 65 | .HasMaxLength(30); 66 | 67 | b.Property("Position") 68 | .IsRequired() 69 | .HasColumnType("nvarchar(20)") 70 | .HasMaxLength(20); 71 | 72 | b.HasKey("Id"); 73 | 74 | b.HasIndex("CompanyId"); 75 | 76 | b.ToTable("Employees"); 77 | }); 78 | 79 | modelBuilder.Entity("Entities.Models.Employee", b => 80 | { 81 | b.HasOne("Entities.Models.Company", "Company") 82 | .WithMany("Employees") 83 | .HasForeignKey("CompanyId") 84 | .OnDelete(DeleteBehavior.Cascade) 85 | .IsRequired(); 86 | }); 87 | #pragma warning restore 612, 618 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/20190927181200_DataBaseCreation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace CompanyEmployees.Migrations 5 | { 6 | public partial class DataBaseCreation : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "Companies", 12 | columns: table => new 13 | { 14 | CompanyId = table.Column(nullable: false), 15 | Name = table.Column(maxLength: 60, nullable: false), 16 | Address = table.Column(maxLength: 60, nullable: false), 17 | Country = table.Column(nullable: true) 18 | }, 19 | constraints: table => 20 | { 21 | table.PrimaryKey("PK_Companies", x => x.CompanyId); 22 | }); 23 | 24 | migrationBuilder.CreateTable( 25 | name: "Employees", 26 | columns: table => new 27 | { 28 | EmployeeId = table.Column(nullable: false), 29 | Name = table.Column(maxLength: 30, nullable: false), 30 | Age = table.Column(nullable: false), 31 | Position = table.Column(maxLength: 20, nullable: false), 32 | CompanyId = table.Column(nullable: false) 33 | }, 34 | constraints: table => 35 | { 36 | table.PrimaryKey("PK_Employees", x => x.EmployeeId); 37 | table.ForeignKey( 38 | name: "FK_Employees_Companies_CompanyId", 39 | column: x => x.CompanyId, 40 | principalTable: "Companies", 41 | principalColumn: "CompanyId", 42 | onDelete: ReferentialAction.Cascade); 43 | }); 44 | 45 | migrationBuilder.CreateIndex( 46 | name: "IX_Employees_CompanyId", 47 | table: "Employees", 48 | column: "CompanyId"); 49 | } 50 | 51 | protected override void Down(MigrationBuilder migrationBuilder) 52 | { 53 | migrationBuilder.DropTable( 54 | name: "Employees"); 55 | 56 | migrationBuilder.DropTable( 57 | name: "Companies"); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/20190927182016_InitialData.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace CompanyEmployees.Migrations 11 | { 12 | [DbContext(typeof(RepositoryContext))] 13 | [Migration("20190927182016_InitialData")] 14 | partial class InitialData 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("Entities.Models.Company", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnName("CompanyId") 29 | .HasColumnType("uniqueidentifier"); 30 | 31 | b.Property("Address") 32 | .IsRequired() 33 | .HasColumnType("nvarchar(60)") 34 | .HasMaxLength(60); 35 | 36 | b.Property("Country") 37 | .HasColumnType("nvarchar(max)"); 38 | 39 | b.Property("Name") 40 | .IsRequired() 41 | .HasColumnType("nvarchar(60)") 42 | .HasMaxLength(60); 43 | 44 | b.HasKey("Id"); 45 | 46 | b.ToTable("Companies"); 47 | 48 | b.HasData( 49 | new 50 | { 51 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 52 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 53 | Country = "USA", 54 | Name = "IT_Solutions Ltd" 55 | }, 56 | new 57 | { 58 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 59 | Address = "312 Forest Avenue, BF 923", 60 | Country = "USA", 61 | Name = "Admin_Solutions Ltd" 62 | }); 63 | }); 64 | 65 | modelBuilder.Entity("Entities.Models.Employee", b => 66 | { 67 | b.Property("Id") 68 | .ValueGeneratedOnAdd() 69 | .HasColumnName("EmployeeId") 70 | .HasColumnType("uniqueidentifier"); 71 | 72 | b.Property("Age") 73 | .HasColumnType("int"); 74 | 75 | b.Property("CompanyId") 76 | .HasColumnType("uniqueidentifier"); 77 | 78 | b.Property("Name") 79 | .IsRequired() 80 | .HasColumnType("nvarchar(30)") 81 | .HasMaxLength(30); 82 | 83 | b.Property("Position") 84 | .IsRequired() 85 | .HasColumnType("nvarchar(20)") 86 | .HasMaxLength(20); 87 | 88 | b.HasKey("Id"); 89 | 90 | b.HasIndex("CompanyId"); 91 | 92 | b.ToTable("Employees"); 93 | 94 | b.HasData( 95 | new 96 | { 97 | Id = new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 98 | Age = 26, 99 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 100 | Name = "Sam Raiden", 101 | Position = "Software developer" 102 | }, 103 | new 104 | { 105 | Id = new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 106 | Age = 30, 107 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 108 | Name = "Jana McLeaf", 109 | Position = "Software developer" 110 | }, 111 | new 112 | { 113 | Id = new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 114 | Age = 35, 115 | CompanyId = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 116 | Name = "Kane Miller", 117 | Position = "Administrator" 118 | }); 119 | }); 120 | 121 | modelBuilder.Entity("Entities.Models.Employee", b => 122 | { 123 | b.HasOne("Entities.Models.Company", "Company") 124 | .WithMany("Employees") 125 | .HasForeignKey("CompanyId") 126 | .OnDelete(DeleteBehavior.Cascade) 127 | .IsRequired(); 128 | }); 129 | #pragma warning restore 612, 618 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/20190927182016_InitialData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace CompanyEmployees.Migrations 5 | { 6 | public partial class InitialData : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.InsertData( 11 | table: "Companies", 12 | columns: new[] { "CompanyId", "Address", "Country", "Name" }, 13 | values: new object[] { new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), "583 Wall Dr. Gwynn Oak, MD 21207", "USA", "IT_Solutions Ltd" }); 14 | 15 | migrationBuilder.InsertData( 16 | table: "Companies", 17 | columns: new[] { "CompanyId", "Address", "Country", "Name" }, 18 | values: new object[] { new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), "312 Forest Avenue, BF 923", "USA", "Admin_Solutions Ltd" }); 19 | 20 | migrationBuilder.InsertData( 21 | table: "Employees", 22 | columns: new[] { "EmployeeId", "Age", "CompanyId", "Name", "Position" }, 23 | values: new object[] { new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 26, new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), "Sam Raiden", "Software developer" }); 24 | 25 | migrationBuilder.InsertData( 26 | table: "Employees", 27 | columns: new[] { "EmployeeId", "Age", "CompanyId", "Name", "Position" }, 28 | values: new object[] { new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 30, new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), "Jana McLeaf", "Software developer" }); 29 | 30 | migrationBuilder.InsertData( 31 | table: "Employees", 32 | columns: new[] { "EmployeeId", "Age", "CompanyId", "Name", "Position" }, 33 | values: new object[] { new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 35, new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), "Kane Miller", "Administrator" }); 34 | } 35 | 36 | protected override void Down(MigrationBuilder migrationBuilder) 37 | { 38 | migrationBuilder.DeleteData( 39 | table: "Employees", 40 | keyColumn: "EmployeeId", 41 | keyValue: new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811")); 42 | 43 | migrationBuilder.DeleteData( 44 | table: "Employees", 45 | keyColumn: "EmployeeId", 46 | keyValue: new Guid("80abbca8-664d-4b20-b5de-024705497d4a")); 47 | 48 | migrationBuilder.DeleteData( 49 | table: "Employees", 50 | keyColumn: "EmployeeId", 51 | keyValue: new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a")); 52 | 53 | migrationBuilder.DeleteData( 54 | table: "Companies", 55 | keyColumn: "CompanyId", 56 | keyValue: new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3")); 57 | 58 | migrationBuilder.DeleteData( 59 | table: "Companies", 60 | keyColumn: "CompanyId", 61 | keyValue: new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870")); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/ExternalClient/20211115113236_ExternalClientData.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace CompanyEmployees.Migrations.ExternalClient 11 | { 12 | [DbContext(typeof(ExternalClientContext))] 13 | [Migration("20211115113236_ExternalClientData")] 14 | partial class ExternalClientData 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .UseIdentityColumns() 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("ProductVersion", "5.0.0"); 23 | 24 | modelBuilder.Entity("Entities.Models.Client", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("uniqueidentifier"); 29 | 30 | b.Property("Name") 31 | .HasColumnType("nvarchar(max)"); 32 | 33 | b.HasKey("Id"); 34 | 35 | b.ToTable("Clients"); 36 | 37 | b.HasData( 38 | new 39 | { 40 | Id = new Guid("c1f33503-bb38-4fa1-98a0-6cfaf9986797"), 41 | Name = "External Client's Test Name" 42 | }); 43 | }); 44 | #pragma warning restore 612, 618 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/ExternalClient/20211115113236_ExternalClientData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace CompanyEmployees.Migrations.ExternalClient 5 | { 6 | public partial class ExternalClientData : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "Clients", 12 | columns: table => new 13 | { 14 | Id = table.Column(type: "uniqueidentifier", nullable: false), 15 | Name = table.Column(type: "nvarchar(max)", nullable: true) 16 | }, 17 | constraints: table => 18 | { 19 | table.PrimaryKey("PK_Clients", x => x.Id); 20 | }); 21 | 22 | migrationBuilder.InsertData( 23 | table: "Clients", 24 | columns: new[] { "Id", "Name" }, 25 | values: new object[] { new Guid("c1f33503-bb38-4fa1-98a0-6cfaf9986797"), "External Client's Test Name" }); 26 | } 27 | 28 | protected override void Down(MigrationBuilder migrationBuilder) 29 | { 30 | migrationBuilder.DropTable( 31 | name: "Clients"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/ExternalClient/ExternalClientContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace CompanyEmployees.Migrations.ExternalClient 10 | { 11 | [DbContext(typeof(ExternalClientContext))] 12 | partial class ExternalClientContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .UseIdentityColumns() 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("ProductVersion", "5.0.0"); 21 | 22 | modelBuilder.Entity("Entities.Models.Client", b => 23 | { 24 | b.Property("Id") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnType("uniqueidentifier"); 27 | 28 | b.Property("Name") 29 | .HasColumnType("nvarchar(max)"); 30 | 31 | b.HasKey("Id"); 32 | 33 | b.ToTable("Clients"); 34 | 35 | b.HasData( 36 | new 37 | { 38 | Id = new Guid("c1f33503-bb38-4fa1-98a0-6cfaf9986797"), 39 | Name = "External Client's Test Name" 40 | }); 41 | }); 42 | #pragma warning restore 612, 618 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Migrations/RepositoryContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace CompanyEmployees.Migrations 10 | { 11 | [DbContext(typeof(RepositoryContext))] 12 | partial class RepositoryContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("ProductVersion", "3.0.0") 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("Entities.Models.Company", b => 23 | { 24 | b.Property("Id") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnName("CompanyId") 27 | .HasColumnType("uniqueidentifier"); 28 | 29 | b.Property("Address") 30 | .IsRequired() 31 | .HasColumnType("nvarchar(60)") 32 | .HasMaxLength(60); 33 | 34 | b.Property("Country") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("Name") 38 | .IsRequired() 39 | .HasColumnType("nvarchar(60)") 40 | .HasMaxLength(60); 41 | 42 | b.HasKey("Id"); 43 | 44 | b.ToTable("Companies"); 45 | 46 | b.HasData( 47 | new 48 | { 49 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 50 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 51 | Country = "USA", 52 | Name = "IT_Solutions Ltd" 53 | }, 54 | new 55 | { 56 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 57 | Address = "312 Forest Avenue, BF 923", 58 | Country = "USA", 59 | Name = "Admin_Solutions Ltd" 60 | }); 61 | }); 62 | 63 | modelBuilder.Entity("Entities.Models.Employee", b => 64 | { 65 | b.Property("Id") 66 | .ValueGeneratedOnAdd() 67 | .HasColumnName("EmployeeId") 68 | .HasColumnType("uniqueidentifier"); 69 | 70 | b.Property("Age") 71 | .HasColumnType("int"); 72 | 73 | b.Property("CompanyId") 74 | .HasColumnType("uniqueidentifier"); 75 | 76 | b.Property("Name") 77 | .IsRequired() 78 | .HasColumnType("nvarchar(30)") 79 | .HasMaxLength(30); 80 | 81 | b.Property("Position") 82 | .IsRequired() 83 | .HasColumnType("nvarchar(20)") 84 | .HasMaxLength(20); 85 | 86 | b.HasKey("Id"); 87 | 88 | b.HasIndex("CompanyId"); 89 | 90 | b.ToTable("Employees"); 91 | 92 | b.HasData( 93 | new 94 | { 95 | Id = new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 96 | Age = 26, 97 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 98 | Name = "Sam Raiden", 99 | Position = "Software developer" 100 | }, 101 | new 102 | { 103 | Id = new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 104 | Age = 30, 105 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 106 | Name = "Jana McLeaf", 107 | Position = "Software developer" 108 | }, 109 | new 110 | { 111 | Id = new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 112 | Age = 35, 113 | CompanyId = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 114 | Name = "Kane Miller", 115 | Position = "Administrator" 116 | }); 117 | }); 118 | 119 | modelBuilder.Entity("Entities.Models.Employee", b => 120 | { 121 | b.HasOne("Entities.Models.Company", "Company") 122 | .WithMany("Employees") 123 | .HasForeignKey("CompanyId") 124 | .OnDelete(DeleteBehavior.Cascade) 125 | .IsRequired(); 126 | }); 127 | #pragma warning restore 612, 618 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.Hosting; 8 | using Microsoft.Extensions.Logging; 9 | 10 | namespace CompanyEmployees 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IHostBuilder CreateHostBuilder(string[] args) => 20 | Host.CreateDefaultBuilder(args) 21 | .ConfigureWebHostDefaults(webBuilder => 22 | { 23 | webBuilder.UseStartup(); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "CompanyEmployees": { 4 | "commandName": "Project", 5 | "launchBrowser": false, 6 | "launchUrl": "weatherforecast", 7 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 8 | "environmentVariables": { 9 | "ASPNETCORE_ENVIRONMENT": "Development" 10 | } 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/Startup.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using CompanyEmployees.Extensions; 3 | using Contracts; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.AspNetCore.HttpOverrides; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Hosting; 10 | using NLog; 11 | using System.IO; 12 | 13 | namespace CompanyEmployees 14 | { 15 | public class Startup 16 | { 17 | public Startup(IConfiguration configuration) 18 | { 19 | LogManager.LoadConfiguration(string.Concat(Directory.GetCurrentDirectory(), "/nlog.config")); 20 | Configuration = configuration; 21 | } 22 | 23 | public IConfiguration Configuration { get; } 24 | 25 | // This method gets called by the runtime. Use this method to add services to the container. 26 | public void ConfigureServices(IServiceCollection services) 27 | { 28 | services.ConfigureCors(); 29 | services.ConfigureIISIntegration(); 30 | services.ConfigureLoggerService(); 31 | services.ConfigureSqlContext(Configuration); 32 | services.ConfigureExternalClientContext(Configuration); 33 | services.ConfigureRepositoryManager(); 34 | services.AddAutoMapper(typeof(Startup)); 35 | 36 | services.AddControllers(); 37 | } 38 | 39 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 40 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerManager logger) 41 | { 42 | if (env.IsDevelopment()) 43 | { 44 | app.UseDeveloperExceptionPage(); 45 | } 46 | else 47 | { 48 | app.UseHsts(); 49 | } 50 | 51 | app.ConfigureExceptionHandler(logger); 52 | app.UseHttpsRedirection(); 53 | app.UseStaticFiles(); 54 | 55 | app.UseForwardedHeaders(new ForwardedHeadersOptions 56 | { 57 | ForwardedHeaders = ForwardedHeaders.All 58 | }); 59 | 60 | app.UseRouting(); 61 | app.UseCors("CorsPolicy"); 62 | 63 | app.UseAuthorization(); 64 | 65 | app.UseEndpoints(endpoints => 66 | { 67 | endpoints.MapControllers(); 68 | }); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/WeatherForecast.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CompanyEmployees 4 | { 5 | public class WeatherForecast 6 | { 7 | public DateTime Date { get; set; } 8 | 9 | public int TemperatureC { get; set; } 10 | 11 | public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); 12 | 13 | public string Summary { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | }, 9 | "ConnectionStrings": { 10 | "sqlConnection": "server=.; database=CompanyEmployeeDb1; Integrated Security=true", 11 | "externalClientConnection": "server=.; database=CompanyEmployeeDb2; Integrated Security=true" 12 | }, 13 | "AllowedHosts": "*" 14 | } 15 | -------------------------------------------------------------------------------- /CompanyEmployees/CompanyEmployees/nlog.config: -------------------------------------------------------------------------------- 1 |  2 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /CompanyEmployees/Contracts/Contracts.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CompanyEmployees/Contracts/IClientRepository.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace Contracts 5 | { 6 | public interface IClientRepository 7 | { 8 | IEnumerable GetAllClients(bool trackChanges); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CompanyEmployees/Contracts/ICompanyRepository.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace Contracts 5 | { 6 | public interface ICompanyRepository 7 | { 8 | IEnumerable GetAllCompanies(bool trackChanges); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CompanyEmployees/Contracts/IEmployeeRepository.cs: -------------------------------------------------------------------------------- 1 | namespace Contracts 2 | { 3 | public interface IEmployeeRepository 4 | { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /CompanyEmployees/Contracts/ILoggerManager.cs: -------------------------------------------------------------------------------- 1 | namespace Contracts 2 | { 3 | public interface ILoggerManager 4 | { 5 | void LogInfo(string message); 6 | void LogWarn(string message); 7 | void LogDebug(string message); 8 | void LogError(string message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CompanyEmployees/Contracts/IRepositoryBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using System.Text; 6 | 7 | namespace Contracts 8 | { 9 | public interface IRepositoryBase 10 | { 11 | IQueryable FindAll(bool trackChanges); 12 | IQueryable FindByCondition(Expression> expression, 13 | bool trackChanges); 14 | void Create(T entity); 15 | void Update(T entity); 16 | void Delete(T entity); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CompanyEmployees/Contracts/IRepositoryManager.cs: -------------------------------------------------------------------------------- 1 | namespace Contracts 2 | { 3 | public interface IRepositoryManager 4 | { 5 | ICompanyRepository Company { get; } 6 | IEmployeeRepository Employee { get; } 7 | 8 | IClientRepository Client { get; } 9 | 10 | void Save(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/Configuration/ClientConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 4 | using System; 5 | 6 | namespace Entities.Configuration 7 | { 8 | public class ClientConfiguration : IEntityTypeConfiguration 9 | { 10 | public void Configure(EntityTypeBuilder builder) 11 | { 12 | builder.HasData 13 | ( 14 | new Client 15 | { 16 | Id = new Guid("c1f33503-bb38-4fa1-98a0-6cfaf9986797"), 17 | Name = "External Client's Test Name" 18 | } 19 | ); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/Configuration/CompanyConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 4 | using System; 5 | 6 | namespace Entities.Configuration 7 | { 8 | public class CompanyConfiguration : IEntityTypeConfiguration 9 | { 10 | public void Configure(EntityTypeBuilder builder) 11 | { 12 | builder.HasData 13 | ( 14 | new Company 15 | { 16 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 17 | Name = "IT_Solutions Ltd", 18 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 19 | Country = "USA" 20 | }, 21 | new Company 22 | { 23 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 24 | Name = "Admin_Solutions Ltd", 25 | Address = "312 Forest Avenue, BF 923", 26 | Country = "USA" 27 | } 28 | ); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/Configuration/EmployeeConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 4 | using System; 5 | 6 | namespace Entities.Configuration 7 | { 8 | public class EmployeeConfiguration : IEntityTypeConfiguration 9 | { 10 | public void Configure(EntityTypeBuilder builder) 11 | { 12 | builder.HasData 13 | ( 14 | new Employee 15 | { 16 | Id = new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 17 | Name = "Sam Raiden", 18 | Age = 26, 19 | Position = "Software developer", 20 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870") 21 | }, 22 | new Employee 23 | { 24 | Id = new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 25 | Name = "Jana McLeaf", 26 | Age = 30, 27 | Position = "Software developer", 28 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870") 29 | }, 30 | new Employee 31 | { 32 | Id = new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 33 | Name = "Kane Miller", 34 | Age = 35, 35 | Position = "Administrator", 36 | CompanyId = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3") 37 | } 38 | ); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/DataTransferObjects/CompanyDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Entities.DataTransferObjects 6 | { 7 | public class CompanyDto 8 | { 9 | public Guid Id { get; set; } 10 | public string Name { get; set; } 11 | public string FullAddress { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/Entities.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/ErrorModel/ErrorDetails.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Text.Json; 5 | 6 | namespace Entities.ErrorModel 7 | { 8 | public class ErrorDetails 9 | { 10 | public int StatusCode { get; set; } 11 | public string Message { get; set; } 12 | 13 | public override string ToString() => JsonSerializer.Serialize(this); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/ExternalClientContext.cs: -------------------------------------------------------------------------------- 1 | using Entities.Configuration; 2 | using Entities.Models; 3 | using Microsoft.EntityFrameworkCore; 4 | 5 | namespace Entities 6 | { 7 | public class ExternalClientContext : DbContext 8 | { 9 | public ExternalClientContext(DbContextOptions options) 10 | : base(options) 11 | { 12 | } 13 | 14 | protected override void OnModelCreating(ModelBuilder modelBuilder) 15 | { 16 | modelBuilder.ApplyConfiguration(new ClientConfiguration()); 17 | } 18 | 19 | public DbSet Clients { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/Models/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Entities.Models 4 | { 5 | public class Client 6 | { 7 | public Guid Id { get; set; } 8 | public string Name { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/Models/Company.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.ComponentModel.DataAnnotations.Schema; 5 | 6 | namespace Entities.Models 7 | { 8 | public class Company 9 | { 10 | [Column("CompanyId")] 11 | public Guid Id { get; set; } 12 | 13 | [Required(ErrorMessage = "Company name is a required field.")] 14 | [MaxLength(60, ErrorMessage = "Maximum length for the Name is 60 characters.")] 15 | public string Name { get; set; } 16 | 17 | [Required(ErrorMessage = "Company address is a required field.")] 18 | [MaxLength(60, ErrorMessage = "Maximum length for the Address is 60 characters.")] 19 | public string Address { get; set; } 20 | 21 | public string Country { get; set; } 22 | 23 | public ICollection Employees { get; set; } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/Models/Employee.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace Entities.Models 6 | { 7 | public class Employee 8 | { 9 | [Column("EmployeeId")] 10 | public Guid Id { get; set; } 11 | 12 | [Required(ErrorMessage = "Employee name is a required field.")] 13 | [MaxLength(30, ErrorMessage = "Maximum length for the Name is 30 characters.")] 14 | public string Name { get; set; } 15 | 16 | [Required(ErrorMessage = "Age is a required field.")] 17 | public int Age { get; set; } 18 | 19 | [Required(ErrorMessage = "Position is a required field.")] 20 | [MaxLength(20, ErrorMessage = "Maximum length for the Position is 20 characters.")] 21 | public string Position { get; set; } 22 | 23 | [ForeignKey(nameof(Company))] 24 | public Guid CompanyId { get; set; } 25 | public Company Company { get; set; } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CompanyEmployees/Entities/RepositoryContext.cs: -------------------------------------------------------------------------------- 1 | using Entities.Configuration; 2 | using Entities.Models; 3 | using Microsoft.EntityFrameworkCore; 4 | 5 | namespace Entities 6 | { 7 | public class RepositoryContext : DbContext 8 | { 9 | public RepositoryContext(DbContextOptions options) 10 | : base(options) 11 | { 12 | } 13 | 14 | protected override void OnModelCreating(ModelBuilder modelBuilder) 15 | { 16 | modelBuilder.ApplyConfiguration(new CompanyConfiguration()); 17 | modelBuilder.ApplyConfiguration(new EmployeeConfiguration()); 18 | } 19 | 20 | public DbSet Companies { get; set; } 21 | public DbSet Employees { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CompanyEmployees/LoggerService/LoggerManager.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using NLog; 3 | 4 | namespace LoggerService 5 | { 6 | public class LoggerManager : ILoggerManager 7 | { 8 | private static ILogger logger = LogManager.GetCurrentClassLogger(); 9 | 10 | public LoggerManager() 11 | { 12 | } 13 | 14 | public void LogDebug(string message) 15 | { 16 | logger.Debug(message); 17 | } 18 | 19 | public void LogError(string message) 20 | { 21 | logger.Error(message); 22 | } 23 | 24 | public void LogInfo(string message) 25 | { 26 | logger.Info(message); 27 | } 28 | 29 | public void LogWarn(string message) 30 | { 31 | logger.Warn(message); 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CompanyEmployees/LoggerService/LoggerService.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CompanyEmployees/Repository/ClientRepository.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using Entities.Models; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace Repository 8 | { 9 | public class ClientRepository : RepositoryBase, IClientRepository 10 | { 11 | public ClientRepository(ExternalClientContext clientContext) 12 | :base(clientContext) 13 | { 14 | } 15 | 16 | public IEnumerable GetAllClients(bool trackChanges) => 17 | FindAll(trackChanges) 18 | .ToList(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CompanyEmployees/Repository/CompanyRepository.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using Entities.Models; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace Repository 8 | { 9 | public class CompanyRepository : RepositoryBase, ICompanyRepository 10 | { 11 | public CompanyRepository(RepositoryContext repositoryContext) 12 | : base(repositoryContext) 13 | { 14 | } 15 | 16 | public IEnumerable GetAllCompanies(bool trackChanges) => 17 | FindAll(trackChanges) 18 | .OrderBy(c => c.Name) 19 | .ToList(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CompanyEmployees/Repository/EmployeeRepository.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using Entities.Models; 4 | 5 | namespace Repository 6 | { 7 | public class EmployeeRepository : RepositoryBase, IEmployeeRepository 8 | { 9 | public EmployeeRepository(RepositoryContext repositoryContext) 10 | :base(repositoryContext) 11 | { 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CompanyEmployees/Repository/Repository.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CompanyEmployees/Repository/RepositoryBase.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Microsoft.EntityFrameworkCore; 3 | using System; 4 | using System.Linq; 5 | using System.Linq.Expressions; 6 | 7 | namespace Repository 8 | { 9 | public abstract class RepositoryBase : IRepositoryBase where T : class where TContext : DbContext 10 | { 11 | protected TContext _context; 12 | 13 | public RepositoryBase(TContext context) => this._context = context; 14 | 15 | public IQueryable FindAll(bool trackChanges) => 16 | !trackChanges ? 17 | _context.Set() 18 | .AsNoTracking() : 19 | _context.Set(); 20 | 21 | public IQueryable FindByCondition(Expression> expression, 22 | bool trackChanges) => 23 | !trackChanges ? 24 | _context.Set() 25 | .Where(expression) 26 | .AsNoTracking() : 27 | _context.Set() 28 | .Where(expression); 29 | 30 | public void Create(T entity) => _context.Set().Add(entity); 31 | 32 | public void Update(T entity) => _context.Set().Update(entity); 33 | 34 | public void Delete(T entity) => _context.Set().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 | --------------------------------------------------------------------------------