Configure One-to-One relationship using Fluent API in Entity Framework Core

Configure One-to-One relationship using Fluent API in Entity Framework Core

Here you will learn how to configure One-to-One Relationship using Fluent API. Note that you can also create this relationship by using EF Core Conventions. The One-to-One Relationship is established by using the HasOne – WithOne pattern.

Let’s create One-to-One relationship between City and CityInformation entities using Fluent API.

The 2 entity classes are:

public class City
{
    public int Id { get; set; }
 
    public string Name { get; set; }
 
    public int CityInformationId { get; set; }
 
    public CityInformation CityInformation { get; set; }
}
 
public class CityInformation
{
    public int Id { get; set; }
 
    public int Population { get; set; }
 
    public string OtherName { get; set; }
 
    public string MayorName { get; set; }
 
    public City City { get; set; }
} 

The City entity has a Reference Navigation Property called CityInformation.

public CityInformation CityInformation { get; set; }

Similary the CityInformation entity has a Reference Navigation Property called City

public City City { get; set; }

Next configure the OnModelCreating method in the DB Context Class of Entity Framework Core as shown below.

public class CompanyContext : DbContext
{
    public DbSet<City> City { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=vaio;Database=Company;Trusted_Connection=True;");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //Write Fluent API configurations here

        modelBuilder.Entity<City>()
            .HasOne(e => e.CityInformation)
            .WithOne(e => e.City)
            .HasForeignKey<City>(e => e.CityInformationId);
    }
}

In the above code I have used the HasOne – WithOne pattern to create One-to-One Relationship between City & CityInformation entities.

You can also create the same relationship the other way around like what I did in the below code.

modelBuilder.Entity<CityInformation>()
            .HasOne(e => e.City)
            .WithOne(e => e.CityInformation)
            .HasForeignKey<City>(e => e.CityInformationId);

On doing the EF Core Migrations the foreign key (one-to-one relationship) is created which is shown in the below image:

one to one relationship fluent api

Understanding One-to-One Relationship creation by Fluent API

Let’s understand it in step by step.

  • modelBuilder.Entity() starts configuring the City entity.
  • The HasOne(e => e.City) method specifies that the City entity includes one CityInformation reference navigation property.
  • The .WithOne(e => e.CityInformation) configures the other end of the relationship, i.e. the CityInformation entity. It specifies that the ‘CityInformation’ entity includes a reference navigation property of ‘City’ type.
  • The .HasForeignKey(e => e.CityInformationId) specifies the foreign key property name which is the CityInformationId column.

Download the source code:

DOWNLOAD

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "ASP.NET Core, jQuery, EF Core, SEO, jQuery, HTML" and more.