C
C#2mo ago
V0FBU1VM

✅ Navigation Property

Any reason as to why I'm getting this message/warning?
The relationship defined by this property contributes to a dependency loop.
The relationship defined by this property contributes to a dependency loop.
11 Replies
Angius
Angius2mo ago
Impossible to tell without code But my bet is you're either using lazy loading or using .Include() to get entities for display
V0FBU1VM
V0FBU1VM2mo ago
@ZZZZZZZZZZZZZZZZZZZZZZZZZ It's appearing when declaring the relationship between entities. For instance here:
public class Shift
{
public int DepartmentId { get; init; }
public Department Department { get; init; }
}
public class Shift
{
public int DepartmentId { get; init; }
public Department Department { get; init; }
}
It's appearing on top of Department. It's working fine. I'm just trying to understand the message properly.
Angius
Angius2mo ago
I guess it's just warning you that if you try to fetch this data in a stupid way you can run into a cyclic reference
V0FBU1VM
V0FBU1VM2mo ago
I see, this is the proper way of doing it right? Or have i got it all wrong.
Pobiega
Pobiega2mo ago
could it be that Department also has a nav prop for List<Shift>?
V0FBU1VM
V0FBU1VM2mo ago
public ICollection<Shift> Shifts { get; set; } = new HashSet<Shift>();
public ICollection<Shift> Shifts { get; set; } = new HashSet<Shift>();
Yes.
Angius
Angius2mo ago
Fetching data? Always .Select() into DTOs
Pobiega
Pobiega2mo ago
Always select into DTOs, and model your DTOs to remove any dependency loops its fine to have reference cycles in your models (not your DTOs), but dont try to serialize that
V0FBU1VM
V0FBU1VM2mo ago
Oh I see, otherwise Shifts will have a department and the department will have shifts and so on and so forth. I get it. Thanks you guys very much.
Pobiega
Pobiega2mo ago
which is fine in terms of C# objects but its not fine in JSON :p
V0FBU1VM
V0FBU1VM2mo ago
Got it. Thanks guys. I really appreciate it.
Want results from more Discord servers?
Add your server