开发者

Entity Framework 4.1 - How to get a name of a column for a generated poco object

I am using entity framework 4.1, an edmx mapping file is used to generate poco classes using T4 templates.

How can I get a name of the data base column out of object context for my poco entity properties (if it is possible at all).

I believe the mapping between properties and columns should be in one of the containers:

var container = objectContext.Metad开发者_JS百科ataWorkspace
    .GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace);
...

But I was not able to identify a link between CSpase and SSpace, it looks like CSSpase might do the job, but this container is empty.

Any ideas?


I used a mixed approached to get the mapping information (we have to implement custom data reader/writer for certain types):

  1. First get EntityType object to extract meta information for your POCO type:

    public static EntityType GetEntityTypeForPoco(this ObjectContext context, 
                                                             Type pocoEntityType)
    {
        EntityType entityType 
                 = context.MetadataWorkspace.GetItem<EntityType>
                   (pocoEntityType.FullName, DataSpace.OSpace);
    
        return (EntityType)context.MetadataWorkspace
               .GetEdmSpaceType((StructuralType)entityType);
    }
    
  2. Read mapping XML document: this part is basically hardcoded, mapping xml document is saved in the assembly where edmx file is, it's called [your_edmx_file_name].msl

    currentMslSchemaDocument = new StreamReader(
                Assembly.GetExecutingAssembly().
                GetManifestResourceStream(CurrentMslSchemaDocumentName)
              ).ReadToEnd();
    
  3. Read property to column mapping from XML document:

    var mappingFragments = MslSchemaDocument
    .Descendants(XName.Get(ElementNameEntityTypeMapping, NamespaceNameMsl))
    .Where(mp => (mp.Attributes(AttributeNameTypeName).Any() 
    && mp.Attribute(AttributeNameTypeName).Value == entityTypeModelName));
    
    if (mappingFragments.Count() == 0)
    {
        throw new Exception(String.Format("Entity mapping {0} is not found in the given schema stream", entityTypeModelName));
    }
    
    //theoretically could be several fragments mapping one entity ytpe to several table
    XElement mappingFragment = mappingFragments.First();
    
    string tableName = mappingFragment.Descendants(XName.Get(ElementNameMappingFragment, NamespaceNameMsl))
    .First().Attribute(AttributeNameStoreEntitySet).Value;
    
    List<KeyValuePair<string, string>> propertyColumnMappingList = new List<KeyValuePair<string, string>>();
    
    foreach (var xScalarProperty in mappingFragment.Descendants(XName.Get(ElementNameScalarProperty, NamespaceNameMsl)))
    {
       propertyColumnMappingList.Add(new KeyValuePair<string, string>(xScalarProperty.Attribute(AttributeNameName).Value,
                xScalarProperty.Attribute(AttributeNameColumnName).Value));
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜