Page 1 of 1

Retrive column name at Runtime

Posted: Mon 12 Mar 2012 23:28
by JoeRuspante
Hi.
In Model First environment, there is any way to know at runtime what Table/column a property is mapped to?

For example, if the property User.Name is mapped to "TBL_USER"."PROP_NAME" on the DB, how can I have this information at runtime?


I need that because I'd like to write a log of every change a user will do in any property of any table.
But I'd like to use the table/column names instead of Classes/Properties names.


Someone can help me?
Thank you in advance.

Posted: Thu 15 Mar 2012 08:34
by Shalex
You can easily set the corresponding naming rules in Entity Developer so that mapping was done in the following way: the name of the class is equal to the name of the table, the name of the property is equal to the name of the column.

Posted: Sun 18 Mar 2012 09:27
by JoeRuspante
Thank you, but I need to have different name between classes and tables.

For example, the "User" class is mapped in the DB as "SYS_UTENTI".
This is because we would like to set special names for some tables (SYS for the system one) that we doesn't like to appear on the model.

The first way I thought is to read the XML generated by the model (the mapping one), but I hope there is a simpler method using the reflection or the DbModel that already read that mapping file.

Posted: Wed 21 Mar 2012 17:14
by Shalex
This question rather should be addressed to the Microsoft team.

It seems like all DbContext's mappings are internal and cannot be easily/conveniently retrieved. Probably, the easiest and quickest solution would be setting attributes with mapping to properties. Are you using Entity Developer? If yes, you can modify the code-generation template by adding generation of attritutes for properties with specifying the column name.