I am mapping my table column name to generic way in to use them in model. like:
public UserEntityMapper()
{
ToTable(\"tbl_User\")
Working on another problem I found a much better way to achieve this. I think this wasn't even possible when I wrote the first answer.
To find a store name starting from a CLR type name, we have to access the store-CLR space of the EDM model. That's where mappings between classes and properties on the one hand, and tables and column on the other hand are found. Knowing this it's just a matter of carefully dissecting the content of a deep tree of objects to get the desired results:
public IEnumerable> GetTableAndColumns()
{
var entityContainerMappings = (this as IObjectContextAdapter).ObjectContext
.MetadataWorkspace.GetItems(DataSpace.CSSpace)
.OfType();
var entityTypeMappings = entityContainerMappings
.SelectMany(m => m.EntitySetMappings
.Where(esm => esm.EntitySet.ElementType.Name == typeof(TEntity).Name))
.SelectMany(esm => esm.EntityTypeMappings).ToArray();
var propertyMappings = (from etm in entityTypeMappings
from prop in etm.EntityType.Properties
join pm in entityTypeMappings.SelectMany(etm => etm.Fragments)
.SelectMany(mf => mf.PropertyMappings)
.OfType()
on prop.Name equals pm.Property.Name
select new {etm, prop, pm}
);
return propertyMappings.Select(x => Tuple.Create(x.etm.EntitySetMapping.EntitySet.Name, x.pm.Column.Name, x.prop.DeclaringType.Name, x.prop.Name));
}
This is a method inside a DbContext subtype. For the specified type, it returns tuples containing
For istance:
Item1 Item2 Item3 Item4
===================================================
Products ProductId Product Id
Products ProductName Product Name
Products QuantityPerUnit Product QuantityPerUnit
Products UnitPrice Product UnitPrice
Products StartDate Product StartDate
Products RowVersion Product RowVersion