dimanche 2 août 2015

Getting a DbContext DbSets using reflection

Was the following not supposed to work, i.e., to retrieve the DbSet<>s ?

Seems others did succeed with this, like here. The code below returns matching elements.

public class MyDb : DbContext
{
    public DbSet<A> A { get; set; }
}

public class A
{
    public string aaa { get; set; }
}

    private static void Main(string[] args)
    {
        MyDb myDb = new MyDb();

        List<PropertyInfo> dbSets = myDb
            .GetType()
            .GetProperties()
            .Where(pi => pi.PropertyType.IsGenericTypeDefinition && pi.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)).ToList();

        Console.WriteLine(dbSets.Count);
    }

//output: 0





Aucun commentaire:

Enregistrer un commentaire