Skip to content

Instantly share code, notes, and snippets.

@mortezadalil
Created August 13, 2023 21:12
Show Gist options
  • Save mortezadalil/72f1a53141d22e09e3e674ce8e7e165b to your computer and use it in GitHub Desktop.
Save mortezadalil/72f1a53141d22e09e3e674ce8e7e165b to your computer and use it in GitHub Desktop.
// Defining a Shadow Property
public class ApplicationDbContext : DbContext
{
public DbSet<Book> Books { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>().Property<DateTime>("ModifiedDate");
}
}
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
}
//Using Shadow Properties in a Query
using (var context = new ApplicationDbContext())
{
var books = context.Books
.OrderByDescending(b => EF.Property<DateTime>(b, "ModifiedDate"))
.ToList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment