❔ 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
3 Replies
antimatter8189
antimatter8189OP2y ago
Guys? 😮
Anton
Anton2y ago
so do you want to add a new record into the table when you change the entity, instead of overwriting it with the new value. Is this what you mean? probably better done with a custom abstraction, or a change interceptor there you could check for phases and mark it as new instead of updated first do it in the simplest waybpossible query FirstOrDefault from the list, and then set the entity to new in the change tracker and maybe reset the id, depends on your model config
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?