代码之家  ›  专栏  ›  技术社区  ›  37Stars

从EDMX文件获取架构

  •  3
  • 37Stars  · 技术社区  · 14 年前

    我需要修改T4模板POCO.tt以从EDMX文件检索数据库模式。我可以看到XML中存储在EntitySet标记中的模式。但是,当使用EntitySet对象时,在任何地方都找不到架构。

    谢谢

    4 回复  |  直到 14 年前
        1
  •  7
  •   NinjaNye    11 年前

    更新 我在博客上写下了我的发现:

    http://www.ninjanye.co.uk/2011/06/getting-schema-information-from-edmx.html

    http://jnye.co/Posts/3/getting-schema-information-from-an-edmx-file-with-poco

    我自己也遇到过同样的问题。 首先,需要从edmx文件的存储模型内容(edmx:StorageModels)部分检索EntityContainer

    在tt模板的顶部(在实例化MetadataLoader并声明inputFile之后),添加以下代码以获取存储模型内容EntityContainer

    StoreItemCollection sic;
    loader.TryCreateStoreItemCollection(inputFile, out sic);
    EntityContainer sicEntityContainer = sic.GetItems<EntityContainer>().First();
    

    然后在foreach(ItemCollection.GetItems…)循环中,您可以使用以下内容获取当前架构

    EntitySet eset = sicEntityContainer.GetEntitySetByName(code.Escape(entity), true);
    string schemaName = eset.MetadataProperties["Schema"].Value.ToString();
    

    注意:您可能需要重复tt模板下面的ComplexType属性的get schema代码

        2
  •  1
  •   Peter T. LaComb Jr.    14 年前

    根据此链接: http://msdn.microsoft.com/en-us/library/cc982042.aspx

    应用程序的架构 目标.NET框架版本4是 定义于 文件。应用程序的架构 以.NET Framework 3.5版为目标 Microsoft.Data.Entity.Design.Edmx_1.xsd公司 文件。

        3
  •  0
  •   Mike Griffin    11 年前

    http://brewdawg.github.io/Tiraggo.Edmx/ 你可以在Visual Studio中通过NuGet安装它,它提供了你的EDMX文件中的所有元数据,微软对你隐藏了这些元数据,非常简单,效果很好。你想要访问所有的低级存储信息,比如你的属性SQL类型、模式等等。您甚至可以使用github repo中的示例Windows.Forms应用程序设置断点并检查数据。

        4
  •  0
  •   K0D4    9 年前

    我正在使用EF6,希望向t4模板生成的类添加一个摘要注释。经过一段时间的修改,我加载了EDMX文件并使用XPath找到了我需要的东西。

    var xmlContent = XDocument.Load(textTransform.Host.ResolvePath(inputFile));
    var edmxNavigator = xmlContent.CreateNavigator();
    XmlNamespaceManager nsMgr = new XmlNamespaceManager(edmxNavigator.NameTable);
    nsMgr.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2009/11/edmx");
    nsMgr.AddNamespace("store", "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator");
    nsMgr.AddNamespace("ssdl", "http://schemas.microsoft.com/ado/2009/11/edm/ssdl");
    nsMgr.AddNamespace("cs", "http://schemas.microsoft.com/ado/2009/11/mapping/cs");
    //This is the loop that came with the default template
    foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
    {
        fileManager.StartNewFile(entity.Name + ".cs");
        BeginNamespace(code);
    
        var mappingAttribute = edmxNavigator.SelectSingleNode("/edmx:Edmx/edmx:Runtime/edmx:Mappings/cs:Mapping/cs:EntityContainerMapping/cs:EntitySetMapping/cs:EntityTypeMapping[@TypeName=\"" + entity.FullName + "\"]/cs:MappingFragment/@StoreEntitySet", nsMgr);
        var entitySet = edmxNavigator.SelectSingleNode("/edmx:Edmx/edmx:Runtime/edmx:StorageModels/ssdl:Schema/ssdl:EntityContainer/ssdl:EntitySet[@Name=\"" + mappingAttribute.Value + "\"]", nsMgr);
        var actualTableName  = (entitySet.SelectSingleNode("@Table") ?? entitySet.SelectSingleNode("@Name")).Value;
        var actualSchemaName = (entitySet.SelectSingleNode("@Schema", nsMgr) ?? entitySet.SelectSingleNode("@store:Schema", nsMgr)).Value;  
    
    #>
    
    <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
    
    /// <summary>
    /// Database Object: <#=actualSchemaName#>.<#=actualTableName#>
    /// </summary>
    <#=codeStringGenerator.EntityClassOpening(entity)#>