samedi 23 janvier 2021

Use reflection to call DbSet methods not working Ef Core

I have a DbContext with a property Countries

public DbSet<Country> Countries { get; set; }

I want to use reflection to call the ToListAsync method

var testMethod = this.Countries.GetType().GetMethod("ToListAsync");
var result = (Task<List<BaseEntity>>)testMethod?.Invoke(this.Countries, null);
if (result != null)
    await result;

This fails at the first hurdle when TestMethod is null

What am I doing wrong?

I am using Ef Core for .NET Core 3.1

Im working towards a method that will take a snapshot of all the tables in a DbContext including filtering expressions

I havent got to the expression part yet because of this method issue but any advise on that would be great

I am going to pass in a dictionary to this snapshot method where the key is the type name and the value is an expression

if the dictionary doesnt contain the type being analysed I will simply need to call the equivalent of

var result = await Countries.ToListAsync()

if the dictionary does contain an expression I need to call Countries.Where().ToListAsync()

Can anyone help please?

Paul





Aucun commentaire:

Enregistrer un commentaire