Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7883271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:29:19+00:00 2026-06-03T04:29:19+00:00

public class MyContext: DbContext { public MyContext() : base(VidallyEF) {} public DbSet<User> Users {

  • 0
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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:29:20+00:00Added an answer on June 3, 2026 at 4:29 am

    There are a few things available on PropertyInfo that will be helpful. IsGenericType will tell you whether the property type is generic and the GetGenericArguments() call will return a Type array containing the types of the generic type parameters.

    foreach (var property in someInstance.GetType().GetProperties())
    {
        if (property.PropertyType.IsGenericType)
        {
            var genericArguments = property.PropertyType.GetGenericArguments();
            //Do something with the generic parameter types                
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following entities: public class User { public virtual ICollection<Role> Roles {
public class Address { public string ZipCode {get; set;} } public class Customer {
public class Options { public FolderOption FolderOption { set; get; } public Options() {
Given this code: public class Car { public virtual int CarId { get; set;
I have a custom context: public class MyContext { public String doSomething() {...} }
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class Test { public static void main(String[] args) { } } class Outer
public class Item { ... } public class Order { public List<Item> Items ...
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class CovariantTest { public A getObj() { return new A(); } public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.