How to query database with EF core in expression way?
I’d like to build a dynamic query according to metadata, and query/edit data in dynamic, probably it could be with expression way, how could I make it?
Target:
db.Blogs.AsNoTracking().FirstOrDefault(p => p.BlogId == 1);
Code?
var funcAsNoTracking = Expression.Call(dbSet.GetType().GetMethod("AsNoTracking")!);
var funcFirstOrDefault = Expression.Call(typeof(IQueryable<>).GetMethod("FirstOrDefault")!);
/* what todo next or else? */
//Code
Other codes:
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; } = new();
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}