Retrieving a schema from an EDMX file

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

Does anyone know where I will find the database schema?

thank

+3
source share
4 answers

UPDATE I wrote 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 myself ran into this problem. First you need to get the EntityContainer from the contents section of the storage model (edmx: StorageModels) in the edmx file

tt ( , MetadataLoader ) , EntityContainer

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

foreach (var entity ItemCollection.GetItems...)

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

. , ComplexType tt

+7

, . edmx - ?

: http://msdn.microsoft.com/en-us/library/cc982042.aspx

, .NET Framework 4 Microsoft.Data.Entity.Design.Edmx_2.xsd . , .NET Framework 3.5 SP1 Microsoft.Data.Entity.Design.Edmx_1.xsd .

% VS100COMNTOOLS% \.. \..\Xml\Schemas\ VS 2010 % VS90COMNTOOLS% \.. \..\Xml\Schemas\( 3.5) VS 2008

+1

http://brewdawg.imtqy.com/Tiraggo.Edmx/, NuGet Visual Studio, EDMX, Microsoft , , . , , SQL-, , . Windows.Forms github .

0

I am working with EF6 and want to add a summary comment to the classes generated by the t4 template. After some hacking time, I managed to do this by downloading 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)#>
0
source

Source: https://habr.com/ru/post/1770353/


All Articles