{"id":17788705,"url":"https://github.com/devel0/skeleton-netcore-ef-code-first","last_synced_at":"2025-04-02T00:29:02.717Z","repository":{"id":122983024,"uuid":"330471278","full_name":"devel0/skeleton-netcore-ef-code-first","owner":"devel0","description":"entity core code-first from console app","archived":false,"fork":false,"pushed_at":"2023-11-18T11:07:00.000Z","size":183,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-07T16:14:55.883Z","etag":null,"topics":["code-first","csharp","database-diagram","entity-framework-core","migrations"],"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/devel0.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}},"created_at":"2021-01-17T19:37:06.000Z","updated_at":"2023-04-30T10:05:42.000Z","dependencies_parsed_at":"2023-03-13T12:16:42.750Z","dependency_job_id":"891923e9-0972-450b-b088-61e7ff65f415","html_url":"https://github.com/devel0/skeleton-netcore-ef-code-first","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devel0%2Fskeleton-netcore-ef-code-first","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devel0%2Fskeleton-netcore-ef-code-first/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devel0%2Fskeleton-netcore-ef-code-first/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devel0%2Fskeleton-netcore-ef-code-first/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devel0","download_url":"https://codeload.github.com/devel0/skeleton-netcore-ef-code-first/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246734136,"owners_count":20825088,"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":["code-first","csharp","database-diagram","entity-framework-core","migrations"],"created_at":"2024-10-27T10:20:54.855Z","updated_at":"2025-04-02T00:29:02.683Z","avatar_url":"https://github.com/devel0.png","language":"C#","readme":"# skeleton-netcore-ef-code-first\n\n- [description](#description)\n- [prepare database context](#prepare-database-context)\n- [managing migrations](#managing-migrations)\n  - [first migrations](#first-migrations)\n  - [update database](#update-database)\n- [run the app](#run-the-app)\n- [migration tool summary](#migration-tool-summary)\n- [clean architecture](#clean-architecture)\n- [how this project was built](#how-this-project-was-built)\n\n:point_right: one-to-one, one-to-many, many-to-many code first relationship examples [here](https://github.com/devel0/skeleton-netcore-ef-code-first/tree/one-to-many#skeleton-netcore-ef-code-first)\n\n## description\n\nShows how to create a code-first local db using sqlite but the same approach can applied using other backends.\n\n## prepare database context\n\n- [add nuget pkg][1] of EntityFrameworkCore.Design and for specific backend used\n- [create table as code first][2]\n- [create db context][3]\n  - [override OnConfiguring][4] where setup specific backend used\n  - [override OnModelCreating][5] where table fields can be configured ( primary keys, indexes, ... ) or seeding of initial data can be added\n  - [create a dataset][6] foreach of the table wants to be materialized on the db\n\n[1]: https://github.com/devel0/skeleton-netcore-ef-code-first/blob/ed27b430cdb5166ac7801ee3b5b493cca64e4bf3/skeleton-netcore-ef-code-first.csproj#L13\n[2]: https://github.com/devel0/skeleton-netcore-ef-code-first/blob/ed27b430cdb5166ac7801ee3b5b493cca64e4bf3/Types/SampleData.cs#L3\n[3]: https://github.com/devel0/skeleton-netcore-ef-code-first/blob/ed27b430cdb5166ac7801ee3b5b493cca64e4bf3/Data/DbContext.cs#L6\n[4]: https://github.com/devel0/skeleton-netcore-ef-code-first/blob/ed27b430cdb5166ac7801ee3b5b493cca64e4bf3/Data/DbContext.cs#L27\n[5]: https://github.com/devel0/skeleton-netcore-ef-code-first/blob/ed27b430cdb5166ac7801ee3b5b493cca64e4bf3/Data/DbContext.cs#L51\n[6]: https://github.com/devel0/skeleton-netcore-ef-code-first/blob/ed27b430cdb5166ac7801ee3b5b493cca64e4bf3/Data/DbContext.cs#L69\n\n## managing migrations\n\nMigrations in code-first db allow team of developers to work on the same db project each with their on local db and versioning their changes through commits that includes `Migrations` folder.\n\nThe first-est migration is done by a developer and acts as an entry point for the work on that database so other developers can add their code-first changes after that commit by adding further migrations including code and `Migrations` folder changes.\n\nBecause each migration has a timestamp in the filename there aren't conflict on these while the `Migrations/LocalDbContextModelSnapshots.cs` can be subjected to normal git conflicts in some circumnstances that can be resolved in the usual way. To reduce conflict situation, regular pulls could help.\n\n### first migrations\n\nInstall dotnet ef migrations tools:\n\n```sh\ndotnet tool install --global dotnet-ef\n```\n\nCreate initial migration:\n\n```sh\ncd skeleton-netcore-ef-core-first\ndotnet ef migrations add initial\n```\n\nCommit the initial migration ( I didn't added the initial migration for didactic purpose ).\n\n### update database\n\nCreate a migration doesn't imply the code-first materialize on database, in order to do that, issue:\n\n```sh\ndotnet ef database update\n```\n\nThis command can be used also to revert a migration applied by specifying a name of a migration already applied and all migrations after that one specified will be reverted returning to a state of \"pending\".\n\nMigrations can be removed, when are in pending state, one by one from tail using `dotnet ef migrations remove`.\n\n## run the app\n\n```sh\ndn run\nStarted with 0 records\nadd new one [newRecord1]\nchanges: SampleData {Id: -9223372036854774807} Added\n```\n\n## migration tool summary\n\n| cmd                                       | description                                                |\n| ----------------------------------------- | ---------------------------------------------------------- |\n| `dotnet ef migrations list`               | list migrations showing which aren't yet applied (pending) |\n| `dotnet ef database update`               | apply pending migrations                                   |\n| `dotnet ef migrations add MIGRATION_NAME` | add migrations                                             |\n| `dotnet ef migrations remove`             | remove latest not yet committed migration                  |\n| `dotnet ef databse update MIGRATION_TO`   | revert applied migration                                   |\n\n## clean architecture\n\nTo improve maintainability, modularity and separation of concerns in enterprise applications the [Clean architecture](https://yoan-thirion.gitbook.io/knowledge-base/software-craftsmanship/code-katas/clean-architecture) should evaluated; there are many boiler plate templates available:\n\n```sh\ndotnet new search clean\n```\n\n## how this project was built\n\n```sh\ndotnet new console -n skeleton-netcore-ef-code-first -f net7.0 --langVersion 11\n\ncd skeleton-netcore-ef-code-first\ndotnet add package Microsoft.EntityFrameworkCore.Design --version 7.0.5\ndotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 7.0.5\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevel0%2Fskeleton-netcore-ef-code-first","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevel0%2Fskeleton-netcore-ef-code-first","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevel0%2Fskeleton-netcore-ef-code-first/lists"}