Entity Framework Core One-to-Many Relationship

Jasmine
5 min readJul 11, 2023

--

Ensuring data consistency is crucial in any application to maintain the integrity and accuracy of the stored information. When working with relationships in Entity Framework Core, there are several ways to enforce data consistency. In this article, we delve into the world of EF Core’s one-to-many relationships which is one of the these ways, exploring its concepts, implementation techniques, and best practices.

Imagine we have two tables such as; Order and OrderItem

Table: Order

Order table

Table: OrderItem

OrderItem table

The “OrderId” column in the “OrderItem” table establishes the relationship with the “Order” table by referencing the primary key “Id” in the “Order” table. The “OrderItem” table has a one-to-many relationship with the “Order” table. Each row in the “OrderItem” table represents a specific item within an order.

In this respresentation, the “Order” represents principal entity or parent, and the “OrderItem” represents depdendent entity or child.

There are three ways to create relations in Entity Framework Core using conventions, Fluent API, and data annotations:

Conventions

In Entity Framework Core, conventions are a set of default rules that help map entity classes to database tables and define the relationships between them. Based on the provided class definitions for “Order” and “OrderItem,” let’s explore how conventions are applied:

public class Order
{
public int Id { get; set; }
public string CustomerName { get; set; }
public DateTime OrderDate { get; set; }

public ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
public int Id { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }

public int OrderID { get; set; }
public Order Order { get; set; }
}
  1. Table Names: By default, EF Core will pluralize the class names to determine the corresponding table names. In this case, the table names will be “Orders” for the “Order” entity and “OrderItems” for the “OrderItem” entity.
  2. Primary Keys: EF Core conventionally assumes that properties named “Id” or “{ClassName}Id” are primary keys. In the “Order” entity, the “Id” property will be recognized as the primary key. Similarly, in the “OrderItem” entity, the “Id” property will serve as the primary key.
  3. Foreign Keys: EF Core conventionally recognizes properties named “{RelatedEntityName}Id” or “{RelatedEntityName}ID” as foreign keys. In the “OrderItem” entity, the “OrderId” property follows this convention, indicating that it is a foreign key referencing the “Id” property in the “Order” entity.
  4. Navigation Properties: EF Core conventionally assumes that properties with the same name as the related entity type or with a name ending in “s” (for collections) represent navigation properties. In the “Order” entity, the “OrderItems” property is a collection navigation property, representing the relationship between an order and its associated order items. In the “OrderItem” entity, the “Order” property is a reference navigation property, representing the relationship between an order item and its associated order.

The “OrderItems” property in the “Order” class is of type ICollection<OrderItem>, representing a collection of order items associated with the order. This collection allows a single "Order" instance to be related to multiple "OrderItem" instances, forming a one-to-many relationship.

The “Order” property in the “OrderItem” class is a reference navigation property of type Order, representing the associated order for the order item. This navigation property establishes the relationship from "OrderItem" back to "Order," completing the one-to-many relationship.

Using conventions in Entity Framework Core is best practice. Follow consistent naming conventions for entities, properties, and relationships. By adhering to standard naming conventions, EF Core can infer and apply default mappings and relationships without requiring explicit configuration.

Fluent API

To create a one-to-many relationship using Fluent API with the “Order” and “OrderItem” classes, you would need to configure the relationship in the OnModelCreating method of your DbContext. Here's an example of how to configure the one-to-many relationship using Fluent API:

public override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.HasMany(o => o.OrderItems) // Order has many OrderItems
.WithOne(oi => oi.Order) // OrderItem has one Order
.HasForeignKey(oi => oi.OrderId); // Foreign key is OrderId in OrderItem
}
  • .HasMany(o => o.OrderItems) defines that an "Order" can have many "OrderItems". This configures the collection navigation property OrderItems in the "Order" class.
  • .WithOne(oi => oi.Order) specifies that an "OrderItem" has only one "Order". This configures the reference navigation property Order in the "OrderItem" class.
  • .HasForeignKey(oi => oi.OrderId) sets the foreign key for the relationship, mapping it to the "OrderId" property in the "OrderItem" class.

Data Annotations

Data annotations are part of the System.ComponentModel.DataAnnotations namespace and provide a declarative way to specify various aspects of your entities, such as primary keys, foreign keys, column names, data validation rules, and more.

The “Order” and the “OrderItem” classes that use data annotaions looks like this:

public class Order
{
[Key]
public int Id { get; set; }
public string CustomerName { get; set; }
public DateTime OrderDate { get; set; }

public ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
[Key]
public int Id { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }

[ForeignKey("Order")]
public int OrderId { get; set; }
public Order Order { get; set; }
}
  • The [Key] attribute is applied to the Id property in both the "Order" and "OrderItem" classes to indicate that they are the primary keys.
  • In the “OrderItem” class, the [ForeignKey("Order")] attribute is applied to the OrderId property to specify that it is the foreign key referencing the "Order" entity.

Data annotations can be a convenient way to apply simple configurations, such as defining primary keys, specifying column names, or enforcing data validation rules. However, for more complex configurations, consider using Fluent API or explicit configurations instead, as they provide more flexibility and are easier to maintain.

Shadow Property:

public class Order
{
public int Id { get; set; }
public string CustomerName { get; set; }
public DateTime OrderDate { get; set; }

public ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
public int Id { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }

public Order Order { get; set; }
}

Ef Core automatically detects Foreign Key using its conventions. The shadow property “OrderId” is introduced to represent the foreign key relationship between “OrderItem” and “Order”. It serves as the foreign key column in the database table but is not explicitly defined in the entity class By utilizing a shadow property, you can represent the relationship between entities without explicitly defining the foreign key property in the entity class. Shadow properties provide a way to handle the relationship at the database level while keeping the entity class clean and encapsulated.

--

--