4

I need to find which table is mapped to an EntityTypeConfiguration class. For example:

  public class PersonMap : EntityTypeConfiguration<Person>
    {
        public PersonMap()
        {
    ...
            this.ToTable("Persons");
    ....

        }
    }

I need something like reverse mapping:

var map=new PersonMap(); 
string table =map.GetMappedTableName();

How can I achieve this?

Saber
  • 2,440
  • 1
  • 25
  • 40

1 Answers1

2

Add the field to PersonMap:

public class PersonMap : EntityTypeConfiguration<Person>
{
    public string TableName { get { return "Persons"; } }
    public PersonMap()
    {
        ...
        this.ToTable(TableName);
        ...
    }
}

Access it like so:

var map = new PersonMap(); 
string table = map.TableName;

If you may not know the type of map, use an interface:

public interface IMap
{
    string TableName { get; }
}
public class PersonMap : EntityTypeConfiguration<Person>, IMap
{
    public string TableName { get { return "Persons"; } }
    public PersonMap()
    {
        ...
        this.ToTable(TableName);
        ...
    }
}

Access like so:

IMap map = new PersonMap(); 
string table = map.TableName;
Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
  • 2
    It was good idea to find TableName without going into EntityTypeConfiguration. But what about Column Names? For example in PersonMap: this.ToTable(TableName); this.Property(t => t.Code).HasColumnName("Code"); this.Property(t => t.Name).HasColumnName("FirstName"); this.Property(t => t.Family).HasColumnName("LastName"); Is there a way to get MappedColumn Name? For example: PersonMap map = new PersonMap(); var pr = map.Property(p => p.Name); string columnName = pr.HasColumnName; //expect FirstName – Saber Sep 25 '12 at 08:18
  • @Saber exactly. This isn't a scalable solution. – Stumblor Sep 17 '15 at 10:19