I'm trying to assign a static List<PropertyInfo>
of all DbSet
properties in the Entities
class.
However when the code runs the List is empty because .Where(x => x.PropertyType == typeof(DbSet))
always returns false.
I tried multiple variations in the .Where(...)
method like typeof(DbSet<>)
, Equals(...)
, .UnderlyingSystemType
, etc. but none works.
Why does .Where(...)
always return false in my case?
My code:
public partial class Entities : DbContext
{
//constructor is omitted
public static List<PropertyInfo> info = typeof(Entities).getProperties().Where(x => x.PropertyType == typeof(DbSet)).ToList();
public virtual DbSet<NotRelevant> NotRelevant { get; set; }
//further DbSet<XXXX> properties are omitted....
}
Since DbSet
is a separate type, you should use a more specific approach:
bool IsDbSet(Type t) {
if (!t.IsGenericType) {
return false;
}
return typeof(DbSet<>) == t.GetGenericTypeDefinition();
}
Now your Where
clause will look like this:
.Where(x => IsDbSet(x.PropertyType))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With