{"id":23055930,"url":"https://github.com/aklaus/dbsample","last_synced_at":"2025-08-15T05:32:27.253Z","repository":{"id":130584909,"uuid":"472179183","full_name":"AKlaus/DbSample","owner":"AKlaus","description":"Example of automated tests against SQL Server with EF Core","archived":false,"fork":false,"pushed_at":"2024-01-01T04:00:18.000Z","size":93,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-01-02T02:41:23.674Z","etag":null,"topics":["docker","dotnet","entity-framework","sql-server","testing","xunit"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AKlaus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-03-21T03:55:35.000Z","updated_at":"2023-12-20T12:51:46.000Z","dependencies_parsed_at":"2023-04-19T21:02:43.782Z","dependency_job_id":null,"html_url":"https://github.com/AKlaus/DbSample","commit_stats":null,"previous_names":[],"tags_count":2,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AKlaus%2FDbSample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AKlaus%2FDbSample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AKlaus%2FDbSample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AKlaus%2FDbSample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AKlaus","download_url":"https://codeload.github.com/AKlaus/DbSample/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229893855,"owners_count":18140620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","dotnet","entity-framework","sql-server","testing","xunit"],"created_at":"2024-12-16T01:13:59.533Z","updated_at":"2025-08-15T05:32:27.241Z","avatar_url":"https://github.com/AKlaus.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Demo project: DbSample\n\n**Example of EntityFramework-based back-end + automated tests against SQL Server in DevOps**\n\n![CI](https://github.com/AKlaus/DbSample/actions/workflows/build_test.yml/badge.svg?branch=main)\n[![Test Coverage](https://coveralls.io/repos/github/AKlaus/DbSample/badge.svg?branch=main)](https://coveralls.io/github/AKlaus/DbSample?branch=main)\n\nProject shows examples of:\n - Automated DB tests for CRUD operations. \n - A build pipeline to execute tests against an SQL Server instance.\n\nSee \"[Pain \u0026 Gain of automated tests against SQL (MS SQL or PostgreSQL)](https://alex-klaus.com/dotnet-sql-tests/)\" article for a deeper explanation of the solution.\n\n## Overview of the solution\n\n### Solution structure\n\n| Project | Description                                                                                                                                                            |\n|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Database     | Database entities, EF DB context and migration.                                                                                                                        |\n| Domain       | Queries and commands, domain layer logic with [Command Query Responsibility Segregation](https://martinfowler.com/bliki/CQRS.html) (CQRS) implementation. |\n| Domain.Tests | Automated tests for querying and updating the entities.                                                                                                                |\n| Api          | The application layer (API).                                                                                                                                           |\n\n### Technologies\n\n - Main project: \n   - [.NET 9](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-9/overview);\n   - [Entity Framework Core 9](https://docs.microsoft.com/en-us/ef/core/) and [dotnet-ef](https://docs.microsoft.com/en-us/ef/core/cli/dotnet) CLI.\n - Test project:\n   - [xUnit](https://xunit.net/) + [Respawn](https://github.com/jbogard/Respawn);\n   - [Docker](https://www.docker.com/) + [SQL Server image](https://hub.docker.com/_/microsoft-mssql-server).\n\n## Getting Started (locally)\n\nFirstly, check out this Git repo and install dependencies:\n - [.NET SDK](https://dotnet.microsoft.com/download) v9.x;\n - [dotnet-ef](https://docs.microsoft.com/en-us/ef/core/cli/dotnet) CLI;\n - [Docker](https://www.docker.com/).\n\n### Prepare SQL Server database\n\n1. Generate SQL script to create the DB schema (see more info in ['./Database'](./Database/) folder)\n```bash\ndotnet ef migrations script -i -o CreateOrMigrateDatabase.sql --project Database/Database.csproj --startup-project Api/Api.csproj --context DataContext -v\n```\n2. Execute a `bash` script that\n   1. launches SQL server in Docker container;\n   2. creates a new database;\n   3. populates the DB with the schema from the provided script;\n   4. sets an environment variable with SQL connection string for consuming in the tests.\n```bash\nsource ./devops/start_docker_sql_server_with_new_db.sh CreateOrMigrateDatabase.sql\n```\n\n*NOTE* that `start_docker_sql_server_with_new_db.sh` specifies the database name and _sa_ password, and also adds an environment variable named `ConnectionString` with the connection string. \u003cbr\u003e\nAlternatively, you can maintain the connection string in [testsettings.json](./Domain.Tests/testsettings.json).\n\n\nHere you go. The SQL Server with an empty database is available.\n\n### Run tests\n\nTo run the tests, make sure that correct connection string is specified in [testsettings.json](./Domain.Tests/testsettings.json) file or environment variables.  \n\nOpen the solution and run the tests from `Domain.Tests` project in your favourite IDE or via a command like\n```bash\ndotnet test --verbosity normal\n```\n\n### Run Web API (Swagger)\n\nTo run the application, the connection string in [appsettings.json](./Api/appsettings.json) file has to point to the right instance of the database.\n\nOpen and launch the solution (the `Api` project). It should open `https://localhost:7135/swagger` in the browser.\n\n## Running tests in DevOps\n\nSee an example build pipeline that runs tests in GitHub Actions – [build_test.yml](./.github/workflows/build_test.yml).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faklaus%2Fdbsample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faklaus%2Fdbsample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faklaus%2Fdbsample/lists"}