Finding getter and setter access modifiers in EdmItemCollection of Entity Framework

I am creating a nice T4 template for a repository template for my objects. Instead of manually parsing the xml in the edmx file, I use EdmItemCollection to create an object graph view for the concept model.

I managed to get a lot of information from this model. But I can't find where to find the Getter and Setter access modifiers. They are present in the CSDL portion of the edmx file.

Example:

<Property Name="CustomerID" Type="String" Nullable="false" MaxLength="5" Unicode="true" FixedLength="true" 
    a:SetterAccess="Public" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" />

      

Where in the object graph should I look for this information?

Here is an example of how I am parsing an object tree.

EdmItemCollection edmItems = new EdmItemCollection(new XmlReader[] { csdlReader });
var ownEntities = from item in edmItems
                  where item.BuiltInTypeKind == BuiltInTypeKind.EntityType
                  select item as EntityTypeBase;

Entities = (from ent in ownEntities // Entities is a property, therefore no declaration
            select new Entity
            {
                Name = ent.Name,
                SetName = (from entSet in entityContainer.BaseEntitySets
                           where (entSet.ElementType == ent) || (ent.BaseType != null && (entSet.ElementType.FullName.Equals(ent.BaseType.FullName)))
                           select entSet.Name).FirstOrDefault(),
                Keys = (from keys in ent.KeyMembers
                        select new Entity.Member
                        {
                            Name = keys.Name,
                            Type = keys.TypeUsage.EdmType.Name 
                        }).ToList(),
                Properties = (from prop in ent.Members
                              select new Entity.Member
                              {
                                  Name = prop.Name,
                                  Type = prop.TypeUsage.EdmType.Name,
                                  IsCollection = prop.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.CollectionType
                              }).ToList()
            }).ToList();

      

Hopefully in this direction I will go.

After reflecting on the Code EdmItemCollection code a lot, it doesn't load the schema http://schemas.microsoft.com/ado/2006/04/codegeneration , so it just ignores those properties.

But I hope someone can help me find out how to find this information?

+1


source to share


1 answer


Thanks to Noam Ben-Ami from Microsoft who pointed me to the CSDL Annotations blog post I can answer my own question.

Anything not directly exposed in the object model from Edm types can still be found in MetadataProperties (which contains every xml property of an element, even those that are represented as typed properties).

I just need to look for MetadataProperty whose name starts with " http://schemas.microsoft.com/ado/2006/04/codegeneration :" and I found it.



To answer my example code:

String codeGenerationXmlns = "http://schemas.microsoft.com/ado/2006/04/codegeneration";
EdmItemCollection edmItems = new EdmItemCollection(new XmlReader[] { csdlReader });
var ownEntities = from item in edmItems
                  where item.BuiltInTypeKind == BuiltInTypeKind.EntityType
                  select item as EntityTypeBase;
var entityContainer = (from item in edmItems
                       where item.BuiltInTypeKind == BuiltInTypeKind.EntityContainer
                       select item as EntityContainer).FirstOrDefault();
Entities = (from ent in ownEntities
            select new Entity
            {
                Name = ent.Name,
                SetName = (from entSet in entityContainer.BaseEntitySets
                           where (entSet.ElementType == ent) || (ent.BaseType != null && (entSet.ElementType.FullName.Equals(ent.BaseType.FullName)))
                           select entSet.Name).FirstOrDefault(),
                IsPublic = ((from metaProps in ent.MetadataProperties
                             where metaProps.Name.Equals(codeGenerationXmlns + ":TypeAccess")
                             select metaProps.Value).FirstOrDefault() ?? "Public").Equals("Public"),
                Keys = (from keys in ent.KeyMembers
                        select new Entity.Member
                        {
                            Name = keys.Name,
                            Type = keys.TypeUsage.EdmType.Name
                        }).ToList(),
                Properties = (from prop in ent.Members
                              select new Entity.Member
                              {
                                  Name = prop.Name,
                                  Type = prop.TypeUsage.EdmType.Name,
                                  IsCollection = prop.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.CollectionType,
                                  PrivateGetter = ((from metaProps in prop.MetadataProperties
                                                    where metaProps.Name.Equals(codeGenerationXmlns + ":GetterAccess")
                                                    select metaProps.Value).FirstOrDefault() ?? "Public").Equals("Private"),
                                  PrivateSetter = ((from metaProps in prop.MetadataProperties
                                                    where metaProps.Name.Equals(codeGenerationXmlns + ":SetterAccess")
                                                    select metaProps.Value).FirstOrDefault() ?? "Public").Equals("Private"),
                              }).ToList()
            }).ToList();

      

+1


source







All Articles