Entity Framework Core 1:n relation causes a loop

I’m using EF Core In-memory database. I have a problem with the definition of the foreign key (navigation property). One user could have multiple tokens. So my user table is called Benutzer and my token table is called RefreshToken:

public class Benutzer
{
    [Key]
    public int BenutzerId { get; set; }

    public string AnmeldeName { get; set; }
    public string Passwort { get; set; }
    public string BevorzugteSprache { get; set; }

    public byte[] Salt { get; set; }

    [NotMapped]
    public int Iterationen { get => 10000; }

    // Navigation property
    // [ForeignKey("BenutzerId")]
    public virtual List<RefreshToken> RefreshTokens { get; set; }
}

public class RefreshToken 
{
    [Key]
    public int RefreshTokenId { get; set; }

    public string Token { get; set; }

    // Navigation property
    public virtual Benutzer Benutzer { get; set; } // <-- caused a loop
    
    [ForeignKey("BenutzerId")]  
    public int BenutzerId { get; set; } 
 }

Without the navigation property in table RefreshToken, it works partially. I can add a token for an user. If I request the user table, I get the attached tokens. But I can add a token with a BenutzerId which is not existing.

Now I added the navigation property to the RefreshToken table, see code above. That has caused a loop.

If I request the user table, I get the attached token, this one attached the user and so on. Further I can add a token with an none existing user.

What’s wrong?

var list = await _dbContext.Set<Benutzer>().ToListAsync();

Thanks a lot

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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; }

Method 2

You appear to be missing the action navigation property, you have the FK but not the entity:

public class RefreshToken 
{
    [Key]
    public int RefreshTokenId { get; set; }

    public string Token { get; set; }

    // Navigation property
    public Benutzer Benutzer {get;set;} <<<<<<<<

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x