{"id":23763887,"url":"https://github.com/tomashubelbauer/ef-fk","last_synced_at":"2026-04-27T08:32:02.257Z","repository":{"id":107986078,"uuid":"171273259","full_name":"TomasHubelbauer/ef-fk","owner":"TomasHubelbauer","description":"Understanding EF Core foreign key model","archived":false,"fork":false,"pushed_at":"2022-04-14T20:09:30.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-25T09:08:33.166Z","etag":null,"topics":["dotnet","dotnet-core","ef","entity-framework","entity-framework-core","foreign-key","net"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomasHubelbauer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-02-18T11:37:56.000Z","updated_at":"2022-04-17T09:53:26.000Z","dependencies_parsed_at":"2023-05-15T00:15:34.801Z","dependency_job_id":null,"html_url":"https://github.com/TomasHubelbauer/ef-fk","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/TomasHubelbauer/ef-fk","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-fk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-fk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-fk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-fk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomasHubelbauer","download_url":"https://codeload.github.com/TomasHubelbauer/ef-fk/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-fk/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32329463,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["dotnet","dotnet-core","ef","entity-framework","entity-framework-core","foreign-key","net"],"created_at":"2024-12-31T22:13:47.761Z","updated_at":"2026-04-27T08:32:02.240Z","avatar_url":"https://github.com/TomasHubelbauer.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EF Core Foreign Key Model\r\n\r\nIn this repository I test whether EF Core will create a foreign key by convention when\r\ndeclaring a model which makes use of a navigation property and letting EF know which\r\nend of the relationship is principal and which is dependant by declaring only a single\r\nconventional ID property.\r\n\r\nFirst I scaffold a .NET Core application and install EF Core. Next I create a database\r\nto test with, utilizing SQL Server LocalDB.\r\n\r\n```powershell\r\ndotnet new console\r\ndotnet add package Microsoft.EntityFrameworkCore\r\ndotnet add package Microsoft.EntityFrameworkCore.SqlServer\r\nsqllocaldb create ef_core_fk_model -s\r\n```\r\n\r\nWith everything set up, let's create a database context class and model classes which\r\nmodel a relationship that I suspect EF will create a FK for.\r\n\r\nThe result: EF does create FKs based on convention.\r\n\r\n## Joins\r\n\r\nNext we demonstrate what sort of connection EF uses when accessing the dependant through\r\nprincipal and vice versa:\r\n\r\n```sql\r\nSELECT [user].[Id], [user].[CarId], [user].[Name], [user.Car].[Id], [user.Car].[Make], [user.Car].[Model]\r\nFROM [Users] AS [user]\r\nINNER JOIN [Cars] AS [user.Car] ON [user].[CarId] = [user.Car].[Id]\r\n```\r\n\r\n```sql\r\nSELECT [car].[Id], [car].[Make], [car].[Model], [car.User].[Id], [car.User].[CarId], [car.User].[Name]\r\nFROM [Cars] AS [car]\r\nLEFT JOIN [Users] AS [car.User] ON [car].[Id] = [car.User].[CarId]\r\n```\r\n\r\n## To-Do\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomashubelbauer%2Fef-fk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomashubelbauer%2Fef-fk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomashubelbauer%2Fef-fk/lists"}