0

Currently trying to find a way to get the type of a generically typed class and have been unable to find a way to do so. My current, working method looks as follows:

public class CategoryConfiguration : EntityTypeConfiguration<Category>
    {
        public CategoryConfiguration()
        {
            ToTable("Categories");
            Property(c => c.Name).IsRequired().HasMaxLength(50);
        }
    }

and I'd like to change this method to be something like:

public class CategoryConfiguration : EntityTypeConfiguration<T>
    {
        public CategoryConfiguration()
        {
            ToTable(/*get string representation of 'T' here and pluralize*/);
            Property(c => c.Name).IsRequired().HasMaxLength(50);
        }
    }
EgoPingvina
  • 734
  • 1
  • 10
  • 33
GregH
  • 5,125
  • 8
  • 55
  • 109

2 Answers2

6

I think you can do:

ToTable(typeOf(T).Name);

And about pluralization please read this

Community
  • 1
  • 1
The One
  • 4,560
  • 5
  • 36
  • 52
1

If you would like to see a class name, use nameof(T). Or, if you would like to see different name bounding with T type, you can use an Attribute, Description for example (read here: https://msdn.microsoft.com/ru-ru/library/system.componentmodel.descriptionattribute(v=vs.110).aspx), and get value of it.

For example:

[Description("/*name, which you would like to see*/")]
public class Category
{
    /* your class realization */
}

And in CategoryConfiguration use

ToTable((typeof(T).GetCustomAttributes(typeof(DescriptionAttribute), true)
                  .First() as DescriptionAttribute)
                  .Description);
EgoPingvina
  • 734
  • 1
  • 10
  • 33