© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
8 replies
antimatter8189

❔ Advice on Data modeling-ef core

Basically I've got a lesson which is an aggregate root, On said lesson i got phases and each phase has an answer.
I want to be able to save past answers aswell to analyze.
Current Code:
      return queryable
                
                .Include(x => x.LessonState)
                .Include(x=>x.Phases).ThenInclude(x=>x.UserAnswer);
        }
      return queryable
                
                .Include(x => x.LessonState)
                .Include(x=>x.Phases).ThenInclude(x=>x.UserAnswer);
        }

So I've come up with 2 approaches:
1.A phase will contain a list of answers and we will only load the last added to the db when making calls, Was unsure how to do it and Chat-gpt gave me this:

return queryable
    .Include(x => x.LessonState)
    .Select(x => new
    {
        Entity = x,
        Phases = x.Phases.Select(p => new
        {
            Phase = p,
            LastUserAnswer = p.UserAnswers.OrderByDescending(ua => ua.Id).FirstOrDefault()
        })
    })
    .ToList()
    .Select(x => new YourEntity
    {
        // Map other properties from x.Entity to the new YourEntity object
        LessonState = x.Entity.LessonState,
        Phases = x.Phases.Select(p => new YourPhaseClass
        {
            // Map other properties from p.Phase to the new YourPhaseClass object
            UserAnswer = p.LastUserAnswer
        }).ToList()
    })
    .AsQueryable();
return queryable
    .Include(x => x.LessonState)
    .Select(x => new
    {
        Entity = x,
        Phases = x.Phases.Select(p => new
        {
            Phase = p,
            LastUserAnswer = p.UserAnswers.OrderByDescending(ua => ua.Id).FirstOrDefault()
        })
    })
    .ToList()
    .Select(x => new YourEntity
    {
        // Map other properties from x.Entity to the new YourEntity object
        LessonState = x.Entity.LessonState,
        Phases = x.Phases.Select(p => new YourPhaseClass
        {
            // Map other properties from p.Phase to the new YourPhaseClass object
            UserAnswer = p.LastUserAnswer
        }).ToList()
    })
    .AsQueryable();

2.create a new entity named pastAnswer that sits on Phase, but doesnt load it except for when we want to load it for something
And when the user submits a new answer, it gets saved as a past answer, and the new answer becomes the user answer
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Advice on data modeling
C#CC# / help
3y ago
Ef core seed (data)
C#CC# / help
12mo ago
Data retrieval EF-core
C#CC# / help
15mo ago
EF-Core data reference [Answered]
C#CC# / help
4y ago