Entity Framework Core 1:n relation causes a loop

The FK definition if on the FK property should point to the navigation property, or if on the navigation property, point to the FK:

public virtual Benutzer Benutzer { get; set; } 
[ForeignKey("Benutzer")]  
public int BenutzerId { get; set; } 

or

[ForeignKey("BenutzerId")]  
public virtual Benutzer Benutzer { get; set; } 
public int BenutzerId { get; set; } 

For EF Core you don’t actually need the FK Property, you can leave it as a Shadow Property to avoid confusion where you might update a reference (Setting a Benutzer vs. changing BenutzerId)

This should work in EF Core with a shadow property:

[ForeignKey("BenutzerId")]  // No property declared for BenutzerId, EF will treat it behind the scenes.
public virtual Benutzer Benutzer { get; set; } 

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top