https://github.com/tomashubelbauer/ef-fk
Understanding EF Core foreign key model
https://github.com/tomashubelbauer/ef-fk
dotnet dotnet-core ef entity-framework entity-framework-core foreign-key net
Last synced: 2 months ago
JSON representation
Understanding EF Core foreign key model
- Host: GitHub
- URL: https://github.com/tomashubelbauer/ef-fk
- Owner: TomasHubelbauer
- Created: 2019-02-18T11:37:56.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2022-04-14T20:09:30.000Z (about 4 years ago)
- Last Synced: 2025-05-25T09:08:33.166Z (about 1 year ago)
- Topics: dotnet, dotnet-core, ef, entity-framework, entity-framework-core, foreign-key, net
- Language: C#
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# EF Core Foreign Key Model
In this repository I test whether EF Core will create a foreign key by convention when
declaring a model which makes use of a navigation property and letting EF know which
end of the relationship is principal and which is dependant by declaring only a single
conventional ID property.
First I scaffold a .NET Core application and install EF Core. Next I create a database
to test with, utilizing SQL Server LocalDB.
```powershell
dotnet new console
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
sqllocaldb create ef_core_fk_model -s
```
With everything set up, let's create a database context class and model classes which
model a relationship that I suspect EF will create a FK for.
The result: EF does create FKs based on convention.
## Joins
Next we demonstrate what sort of connection EF uses when accessing the dependant through
principal and vice versa:
```sql
SELECT [user].[Id], [user].[CarId], [user].[Name], [user.Car].[Id], [user.Car].[Make], [user.Car].[Model]
FROM [Users] AS [user]
INNER JOIN [Cars] AS [user.Car] ON [user].[CarId] = [user.Car].[Id]
```
```sql
SELECT [car].[Id], [car].[Make], [car].[Model], [car.User].[Id], [car.User].[CarId], [car.User].[Name]
FROM [Cars] AS [car]
LEFT JOIN [Users] AS [car.User] ON [car].[Id] = [car.User].[CarId]
```
## To-Do