Home >Backend Development >C++ >How to Handle Multiple Foreign Keys from the Same Table in Entity Framework Code First?

How to Handle Multiple Foreign Keys from the Same Table in Entity Framework Code First?

Susan Sarandon
Susan SarandonOriginal
2025-01-29 08:18:12371browse

How to Handle Multiple Foreign Keys from the Same Table in Entity Framework Code First?

The physical framework code is preferred: handle multiple outer keys from the same table

In the physical framework code first, you may encounter a relationship that needs to be established between two entities. These two entities have multiple external keys that point to the same table. This is particularly challenging for beginners.

Considering a model involving teams and games, each of which has a home team and a guest team. However, trying to use traditional external keys to create this model may cause cycle reference errors.

Understand the problem

As shown in the original code fragment, when the outer key is defined in the Match entity, you actually create a pair of multi -relationships for hometeam and Guestteam attributes between Match and Team. However, this setting will cause a circular reference, because Team also references Match through the external key. This cycle violates the constraints of the database.

Solution: Use set navigation attributes and ModelBuilder Fluent API

In order to solve this problem, you can use an improved model that uses a collection of navigation attributes and uses the ModelBuilder Fluent API with the physical framework. This is an example:

In the Team class, define two separate collection navigation attributes:

In the MATCH class, delete the ForeIGNKEY attribute:

In the DBContext class, rewrite the onmodelcreating method and use the FLUENT API configuration relationship:

<code>public virtual ICollection<Match> HomeMatches { get; set; }
public virtual ICollection<Match> AwayMatches { get; set; }</code>

In this updated model, the MATCH entity has two external key attributes, but there is no navigation attribute. Instead, the navigation attribute is defined on the Team entity and allows traversing from Team to Match. The model creation process uses Fluent API to explicitly specify the relationship to prevent circulation reference. In addition, the WillcascadeondElete attribute is set to False to prevent class deletion, which is not recommended in this scene.

<code>public int HomeTeamId { get; set; }
public int GuestTeamId { get; set; }</code>
By following this method, you can successfully create a model with multiple external bonds pointed to the same table in the physical framework code first.

The above is the detailed content of How to Handle Multiple Foreign Keys from the Same Table in Entity Framework Code First?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn