public class MyContext: DbContext
{
public MyContext() : base("VidallyEF") {}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Contest> Contests { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Submission> Submissions { get; set; }
}
I’m trying to iterate through the properties of MyContext, and then through the properties of each of those properties. I have this:
foreach (var table in typeof(MyContext).GetProperties())
{
// TODO add check that table is DbSet<TEntity>..not sure..
PropertyInfo[] info = table.GetType().GetProperties();
foreach (var propertyInfo in info)
{
//Loop
foreach (var attribute in propertyInfo.GetCustomAttributes(false))
{
if (attribute is MyAttribute)
{
//do stuff
}
}
}
}
The problem is that because the Properties of MyContext are generics, the GetType().GetProperties() is not returning the properties of the underlying objects. Some how I need to get to User and Role object.
Any help would be appreciated,
Thanks
There are a few things available on PropertyInfo that will be helpful.
IsGenericTypewill tell you whether the property type is generic and theGetGenericArguments()call will return aTypearray containing the types of the generic type parameters.It’s also important to note that
GetProperties()only returns the properties available on the specified type. If you want types that may be contained or used by your specified type, you will have to do a little bit of digging to get them.