开发者

Getting the Schema from an EDMX file

开发者 https://www.devze.com 2023-01-20 16:36 出处:网络
I need to mod开发者_运维知识库ify 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 schem

I need to mod开发者_运维知识库ify 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


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


I think I misunderstood your question the first time. Have you examined the edmx schema for any clues?

According to this link: http://msdn.microsoft.com/en-us/library/cc982042.aspx

The schema for applications that target the .NET Framework version 4 is defined in the Microsoft.Data.Entity.Design.Edmx_2.xsd file. The schema for applications that target the .NET Framework version 3.5 SP1 is defined in the Microsoft.Data.Entity.Design.Edmx_1.xsd file.

Those are in %VS100COMNTOOLS%\..\..\Xml\Schemas\ for VS 2010, and %VS90COMNTOOLS%\..\..\Xml\Schemas\ (the 3.5 only) for VS 2008


I'm working with EF6 and wanted to add a summary comment to the classes being generated by the t4 template. After hacking around for a while, I managed to do it by loading the EDMX file and using XPath to find what I needed.

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)#>


See http://brewdawg.github.io/Tiraggo.Edmx/ you can install it via NuGet within Visual Studio and it serves up ALL of the metadata from your EDMX files that Microsoft hides from you, very simple, works great. You want access to all that low level storage information like your property SQL types, the schema, it's all there. You can even use the Sample Windows.Forms app in the github repo to set a breakpoint and examine the data.


Facing this issue a few weeks ago. As a result create Xslt transformation of EDMX to XSD. https://wash-inside-out.blogspot.com/2022/12/edmx-file-to-xsd-with-xslt.html

0

精彩评论

暂无评论...
验证码 换一张
取 消