Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

C#–Get EDMX Property Names and Class Names of Entities

Posted by Paul on June 7, 2012


This is a short tip to get the property name sand class names of entities inside an EDMX Context.

For eg: you have context name as MyContext and you have mapped 2 tables Article  and Person

The property names will be: Articles, People (getting pluralized)

The class names will be still: Article, Person

Please let me know any queries you have.

code to get property names:

        private void button1_Click(object sender, EventArgs e)
        {
            listBox.DataSource = GetPropertyNames(typeof(MyContext));
        }

        public IList<string> GetPropertyNames(Type type)
        {
            return type.GetProperties().Where(p => (p.PropertyType.Name  == "ObjectSet`1") && (p.PropertyType.GetGenericArguments().Count() == 1)).Select(p => p.Name).ToList();
 }

code to get class names:

        private void button2_Click(object sender, EventArgs e)
        {
            listBox.DataSource = GetClassNames(typeof(MyContext ));
        }

        private object GetClassNames(Type type)
        {
            return type.GetProperties().Where(p => (p.PropertyType.Name == "ObjectSet`1") && (p.PropertyType.GetGenericArguments().Count() == 1)).Select(p => p.PropertyType.GetGenericArguments().First().Name).ToList();
         }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s