How do you query EF Core temporal tables for created date

1k views Asked by At

I recently implemented EF Core temporal tables for a project that I am working on. No problems with migrations.

I have a requirement to display record information in a datagrid to include when a record was created.

Getting the date that the record was modified is easy, but I am stuck on how to get the date of when the record was created using a linq query.

Any ideas?

1

There are 1 answers

0
torrey garland On

i figured it out. here is the code in case someone else needs it.

not sure if this is the best way to do it, but it worked with no errors on with EF Core 6

 return await (from user in context.Users.AsExpandable().AsNoTracking()
               let updatedOn = EF.Property<DateTime>(user, PeriodStart)
               where predicate.Invoke(user)
               orderby user.Email
               select new RowModel(
                          user.Id,
                          user.UserName,
                          user.Email,
                          (from history in context.Users.TemporalAll()
                           where history.Id == user.Id
                           let createdOn = EF.Property<DateTime>(history, PeriodStart)
                           orderby createdOn
                           select (DateTime?)createdOn).FirstOrDefault() ?? updatedOn,
                          updatedOn,
                          user.EmailConfirmed || user.PhoneNumberConfirmed,
                          user.LockoutEnd < now)).ToArrayAsync(cancellationToken);