❔ EFCORE DESIGN ONE TO MANY

Can you guys help me to design a correct one to many relationships in this case The problem I have this LINQ below
public async Task<Conversation?> FindByNameAsync(string name, CancellationToken cancellationToken = default)
=> await _dbSet.Include(conversation => conversation!.ChatMessages)
.ThenInclude(chatMessage => chatMessage.User)
.Take(10).FirstOrDefaultAsync(x => x.Name == name!, cancellationToken);
public async Task<Conversation?> FindByNameAsync(string name, CancellationToken cancellationToken = default)
=> await _dbSet.Include(conversation => conversation!.ChatMessages)
.ThenInclude(chatMessage => chatMessage.User)
.Take(10).FirstOrDefaultAsync(x => x.Name == name!, cancellationToken);
This one will be cycle because it's include the User and the User have ChatMessages like in the image so this will cause recursive cycle when loading the data My solution (but fail) On User.cs I could remove the below properties
public virtual ICollection<ChatMessage> ChatMessages { get; set; } = new HashSet<ChatMessage>();
public virtual ICollection<ChatMessage> ChatMessages { get; set; } = new HashSet<ChatMessage>();
But I end up need it for setup Delete Cascade behavior
builder.Entity<ChatMessage>(entity =>
{
entity.HasOne(i => i.User).WithMany(s => s!.ChatMessages).OnDelete(DeleteBehavior.Cascade);
});
builder.Entity<ChatMessage>(entity =>
{
entity.HasOne(i => i.User).WithMany(s => s!.ChatMessages).OnDelete(DeleteBehavior.Cascade);
});
Any suggestion ?
6 Replies
D.Mentia
D.Mentia2y ago
that doesn't result in recursion because a. You haven't explicitly done .Include(ChatMessages).ThenInclude(User).ThenInclude(ChatMessages).ThenInclude(User)... (forever) and b. because EF doesn't do more queries when it already knows the data it needs (in this case, the original user and/or ChatMessage, which it will put on the ChatMessage/User entity where appropriate, but it won't perform new queries if it already has it). Also c. it will be the same instance (if it's the same User) each time, so you're not going to run out of stack space or anything The entities it gives you will appear recursive, and if you want to like json serialize them you're gonna have to use serialization options to ignore recursion, but that doesn't mean the SQL query itself is recursive
Saber
Saber2y ago
Who said the sql query is recursive. The json serialization of the object is recursive.
D.Mentia
D.Mentia2y ago
that just means there should be a non-recursive DTO that's being serialized instead of the db entity, not that the db should be restructured. If that's the only problem
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
TotechsStrypper
TotechsStrypperOP2y ago
Yes
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?