entity-framework-4t4

Getting the Schema from an EDMX file


I need to modify the T4 template POCO.tt to retrieve the database schema from the EDMX file. I can see the schema stored in an EntitySet tag in the XML. However I cannot find the schema anywhere when using an EntitySet object.

Anyone know where I would find the database schema?

Thanks


Solution

  • UPDATE I wrote up my findings on this in a blog post:

    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

    I came across this same problem myself. First you need to retrieve the EntityContainer from the Storage Model Content (edmx:StorageModels) section of the edmx file

    At the top of the tt template (after the MetadataLoader is instantiated and the inputFile is declared) add the following code to get the Storage Model Content EntityContainer

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

    Then from within the foreach (var entity in ItemCollection.GetItems...) loop you can get the current schema with the following

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

    Note: You may have to repeat the get schema code for ComplexType properties lower down in the tt template