Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 974
37Stars Avatar asked Feb 27 '23 00:02

37Stars


1 Answers

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

like image 76
NinjaNye Avatar answered Apr 05 '23 02:04

NinjaNye