Column name
To get the name of a column, you must first get EdmPropertyassociated with that column in "structural space" ( SSpace). I provide the code for this below. If you have EdmProperty, the column name will be simple EdmProperty.Name:
string GetColumnName(DbContext context, PropertyInfo property) {
return GetStructuralSpaceEdmProperty(context, property).Name;
}
Structural Space Property
This is based on the article . This article gives you enough information to compare with structural EntityType. I added a little at the end to do the actual mapping of the properties to get a EdmPropertyrepresenting column. As the article says, these APIs require ≥EntityFramework-6.1.
EdmProperty GetStructuralSpaceEdmProperty(DbContext context, PropertyInfo property) {
IObjectContextAdapter adapter = context;
var metadata = adapter.ObjectContext.MetadataWorkspace;
var objectItemCollection = (ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace);
var objectEntityType = metadata.GetItems<EntityType>(DataSpace.OSpace)
.Single(oet => objectItemCollection.GetClrType(oet) == property.DeclaringType);
var conceptualEntityType = metadata.GetItems<EntityType>(DataSpace.CSpace)
.Single(cet => objectEntityType.Name == cet.Name);
var conceptualEdmProperty = conceptualEntityType.Properties
.Single(ep => ep.Name == property.Name);
var entitySet = metadata.GetItems<EntityContainer>(DataSpace.CSpace)
.Single().EntitySets
.Single(es => es.ElementType.Name == conceptualEntityType.Name);
var entityMapping = metadata.GetItems<EntityContainerMapping>(DataSpace.CSSpace)
.Single().EntitySetMappings
.Single(esm => esm.EntitySet == entitySet);
var fragments = entityMapping.EntityTypeMappings
.SelectMany(etm => etm.Fragments);
var propertyMappings = fragments.SelectMany(f => f.PropertyMappings);
var structuralSpaceProperty = propertyMappings
.OfType<ScalarPropertyMapping>()
.Single(pm => pm.Property == conceptualEdmProperty).Column;
return structuralSpaceProperty;
}
, EdmProperty , . , SQL Server EdmProperty.IsUnicode true NVARCHAR/NCHAR false VARCHAR/CHAR .
EF
. " 10 - Entity Framework" API, . DataSpace.OSpace " ", POD.net. DataSpace.SSpace " ", , , "" "SQL" , , , . DataSpace.CSpace " ", , , , " ", " ". DataSpace.OCSpace . , , , .net. DataSpace.CSSpace . , API ColumnAttribute.
API
API EF, -, , API EF. , . , , Enumerable.OfType<TResult> ScalarPropertyMapping , , ScalarPropertyMapping ScalarPropertyMapping. , MetadataWorkspace.GetItems<T>() , , , , EntityType. , EF , API.