Natural Keys and Surrogate Keys in Entity Framework

In a nutshell, this post is going to be about using natural and surrogate keys in Entity Framework.

Definitions and a Simple Example

A surrogate key is a key (the type doesn’t really matter) with no business meaning that is used to uniquely identify a record in a table. A natural key, on the other hand, DOES have a business meaning. Let’s look at an example.

public class Country
{		
    public int CountryId { get; set; }
    public string Abbreviation { get; set; }
    public string Name { get; set; }
}

In the above example, Country.CountryId is a primary key (obviously) that has no business meaning. Its only purpose is to identify a country record. Hence, CountryId is a surrogate key.

Now let’s look at the same entity rewritten to use the natural key:

[Index(nameof(Name), IsUnique = true)]
public class Country
{		
    public int CountryId { get; set; }
    public string Abbreviation { get; set; }
    public string Name { get; set; }
}

Wait, something is not right. Why CountryId is still there? For the sake of the demonstration, imagine that it’s there because Country entity already exists in the system and you don’t want to refactor the rest of the system to remove it right now (often it’s not an option). But then if the Country entity already exists in the system in it’s original form, why would we even consider introducing a natural key?

To answer that question, let’s introduce a NEW Guest entity:

public class Guest
{       
    public int GuestId { get; private set; }
    
    // TODO: need to reference country
}

Later we need to reference a Country. As a quick win we could add a CountryId property to Guest entity (using a surrogate key reference). We’ve probably all configured Entity Framework to work this way, so I will skip the configuration code for this. Instead, let us follow up on this and add a new requirement by saying that the corresponding UI needs to list the guest information including country of origin (e.g. for the invoicing purposes). That means our backend code would look somewhat like this:

dbContext.Guests.Include(g => Country);

or we could use projection with AutoMapper:

dbContext.Guests.ProjectTo<GuestDto>()

It doesn’t matter which option you choose. The underlying T-SQL query generated by Entity Framework will still include the JOIN Country statement. But that is only the case when we are using surrogate keys! Let us redesign the Guest entity to directly reference Country via a unique, natural key:

public class Guest
{       
    // ...
    public string CountryName { get; private set; }
    // ...
}

No additional JOIN statement is not needed for the query above. The country name is simply part of the Guest entity. Before we run it we need to configure EF to handle this new relationship accordingly:

// EntityTypeBuilder<Guest> entity
entity
    .HasOne<Country>()
    .WithMany()
    .HasForeignKey(g => g.CountryName)
    .HasPrincipalKey(c => c.Name)

// EntityTypeBuilder<Country> entity
entity
    .HasIndex(c => c.Name)
    .IsUnique();

Does Using Natural Keys Affect Performance?

In general, no. I do not expect that the performance will ever be an issue, but it can happen in certain circumstances. You should approach this choice the same way when choosing an ORM over raw SQL or relational database over NoSQL.

When to Absolutely Avoid Using Natural Keys?

You have to be aware that natural keys are ‘owned’ by a business. Should it ever require renaming them, you’re gonna go bald in an instant from the stress (records in the parent AND the child tables will have to be updated). Remember that there is NEVER a guarantee that the choices you make will prove bulletproof over time. Even the Country example above isn’t immune to changes say as Turkey recently changing its name to Türkiye.

Picture of Piotr Kolodziej

Piotr Kolodziej

Born and raised in Poland, Piotr did his first programming on Atari 800XL. He covered his first dial-up modem with a duve, so his mom wouldn’t hear it and freak over the internet bills. He graduated with a degree in Telecommunication and Business Application Programming. Piotr is a certified .NET and Azure Developer, and is passionate about excellent software architecture.

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Using Respawn with Integration Testing

Integration Testing In software testing, integration tests are used to test the interaction between different parts of the system, such as the interaction between the database and other APIs and services. Observing the testing pyramid, inte

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.