1

我一直在为我的实体的存储库模式创建一个不错的 T4 模板。我没有手动解析 edmx 文件中的 xml,而是使用 EdmItemCollection 为概念模型创建对象图表示。

我已经能够从这个模型中获得很多信息。但是我找不到 Getter 和 Setter 访问修饰符的位置。它们存在于 edmx 文件的 CSDL 部分。

例子:

<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" />

我应该在对象图中的哪个位置查找此信息?

这是我如何解析对象树的示例。

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();

我希望很清楚我要去的方向。

在通过 EdmItemCollection 的代码进行大量反射后,它不会加载http://schemas.microsoft.com/ado/2006/04/codegeneration架构,因此它只是忽略了这些属性。

但我希望有人可以帮助我找出如何找到这些信息?

4

1 回答 1

1

感谢来自 Microsoft 的 Noam Ben-Ami,他向我介绍了 CSDL 中的注释博客文章,我能够回答我自己的问题。

Edm 类型中没有直接呈现在对象模型中的所有内容仍然可以在 MetadataProperties 中找到(它包含元素的每个 xml 属性,即使是表示为类型化属性的那些)。

我只需要查找名称以“ http://schemas.microsoft.com/ado/2006/04/codegeneration :”开头的 MetadataProperty 并且我找到了它。

回答我的代码示例:

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();
于 2008-11-21T10:12:52.407 回答