You are currently viewing EF Core Relationships Made Simple: Primary Keys, Foreign Keys, and Navigation Properties

EF Core Relationships Made Simple: Primary Keys, Foreign Keys, and Navigation Properties

Database relationships sounded much more complicated to me when people started throwing around terms like:

  • primary key
  • foreign key
  • navigation property
  • one-to-many
  • many-to-many

But the basic idea is something we already understand in real life:

Things belong to other things.

For example:

One Account
   ↓
Many Transactions

That is a relationship.

Start With the Primary Key

Suppose we have an Account entity:

public class Account
{
    public int Id { get; set; }

    public string Name { get; set; } = string.Empty;
}

Id is the primary key.

The primary key uniquely identifies one row.

Example:

IdName
1Checking
2Savings

Account 1 and Account 2 can have similar data, but their IDs are different.

I think of the primary key as:

This row’s unique identity.

What Is a Foreign Key?

Now imagine transactions:

IdAccountIdAmount
1011-45.20
10211500.00
1032200.00

Look at AccountId.

That is a foreign key.

It points back to the Accounts table.

Transactions.AccountId
         ↓
     Accounts.Id

So Transaction 101 belongs to Account 1.

Transaction 103 belongs to Account 2.

One-to-Many

This relationship is called one-to-many.

Account 1
   ├── Transaction 101
   ├── Transaction 102
   └── Transaction 108

One account can have many transactions.

But each transaction belongs to one account.

ONE Account
     ↓
MANY Transactions

Representing It in C#

The Account entity might look like:

public class Account
{
    public int Id { get; set; }

    public string Name { get; set; } = string.Empty;

    public ICollection<Transaction> Transactions
        { get; set; } = new List<Transaction>();
}

And Transaction:

public class Transaction
{
    public int Id { get; set; }

    public decimal Amount { get; set; }

    public int AccountId { get; set; }

    public Account Account { get; set; } = null!;
}

Now we have both:

public int AccountId { get; set; }

and:

public Account Account { get; set; }

They look related because they are.

Foreign Key vs Navigation Property

This distinction helped me a lot.

public int AccountId { get; set; }

is the foreign-key value.

For example:

AccountId = 7

But:

public Account Account { get; set; }

is a navigation property.

It lets C# code navigate from the transaction object to the related account object.

Conceptually:

Transaction
 ├── AccountId = 7
 │
 └── Account
       ↓
    Account object

The foreign key stores the relationship in the database.

The navigation property makes that relationship convenient to use in C#.

Navigation in the Other Direction

The Account entity can also have:

public ICollection<Transaction> Transactions
    { get; set; } = new List<Transaction>();

That lets us navigate the other way:

Account
  ↓
Transactions collection
  ↓
Transaction
Transaction
Transaction

So:

Transaction.Account

can get the account for a transaction.

And:

Account.Transactions

can represent the transactions belonging to the account.

Loading Related Data With Include()

Suppose I query an account:

var account = await _context.Accounts
    .FirstOrDefaultAsync(a => a.Id == id);

Depending on how the application is configured, its Transactions collection may not automatically be loaded.

If I need related transactions, I can use:

var account = await _context.Accounts
    .Include(a => a.Transactions)
    .FirstOrDefaultAsync(a => a.Id == id);

I read this like:

Get the account, and include its transactions.

Accounts
   ↓
Include Transactions
   ↓
Account + related Transactions

Why Relationships Matter

Without relationships, I could end up storing duplicate information.

Imagine putting the entire account name inside every transaction row:

TransactionIdAccountNameAmount
1Checking Account-10
2Checking Account-20
3Checking Account-30

What happens if the user renames the account?

Now I may have to update the same name in hundreds of places.

Instead:

Accounts

Id = 1
Name = Checking Account

and:

Transactions

AccountId = 1
AccountId = 1
AccountId = 1

The relationship connects the records.

That is a major idea behind relational databases.

Another Example: Category and Transactions

A category could also have many transactions.

Category: Groceries
       ↓
 ┌─────┼────────┐
 ↓     ↓        ↓
T1     T2       T3

The transaction could contain:

public int? CategoryId { get; set; }

public Category? Category { get; set; }

The ? means a transaction may be allowed to have no category.

Now we are expressing another business rule through the data model.

What About Many-to-Many?

Suppose a transaction can have many tags:

Transaction
  ↓
Food
Vacation
Family

And one tag can belong to many transactions.

Tag: Vacation
   ↓
Transaction 5
Transaction 12
Transaction 30

That is many-to-many.

Conceptually:

Transactions
     ↕
TransactionTags
     ↕
Tags

Modern EF Core can manage many-to-many relationships in convenient ways, but underneath the relational database still needs a way to connect the records.

What I Would Say in an Interview

If someone asked:

“What is the difference between a primary key and a foreign key?”

I would say:

A primary key uniquely identifies a record in its own table. A foreign key stores the primary-key value of a related record and creates a relationship between the tables.

And if they asked about navigation properties:

Navigation properties let C# code work with related entity objects, while the foreign-key property stores the actual key used for the relationship.

The short version I remember is:

Primary Key
Who am I?

Foreign Key
Who do I belong to?

Navigation Property
Let me get that related object.

Once I started seeing database relationships as connections between normal objects instead of abstract database vocabulary, EF Core relationships made much more sense.

Leave a Reply