none
How can I specify the XElement attribute prefix for an EF edmx <EntitySet> view type? RRS feed

  • Question

  • Using C# on a Win7 machine I am needing to dynamically change the schema type on an Entity Framework 6.2 edmx xml file prior to instansiating EF. I can filter the edmx Xml for the "Schema" attribute for tables but not for views. Tables use the non prefixed Attribute "Schema" and Views for some odd reason use the prefixed Attribute "store:Schema".

    Unfortunately if I filter Attributes for the obvious "store:Schema" I get an XmlException saying "The ':' character, hexadecimal value 0x3A, cannot be included in a name.". I've tried various ways to filter for this needed Attribute so that I can change the Attribute to my custom schema name so that I can later have EF run a view from another schema than "dbo". My XML code is below. How can I filter the storage Descendants for a prefixed Attribute?

            // How can I specify the schema prefix for an EF edmx <EntitySet> view type?
            // Table <EntitySet> has a non prefixed attribute call "Schema"
            // EF view <EntitySet> has a prefixed attribute called "store:Schema" that I cannot seem to filter for
            // Filtering with "store:Schema" throws an XmlException
            //  - "The ':' character, hexadecimal value 0x3A, cannot be included in a name."
            //
            // Change the schema for all the tables and views from the dbo (or whatever) EF specified schema (if any)
            // to the desired schema.
            foreach (var entitySet in storageXml.Descendants(storageNS + "EntitySet"))
            {
                // this works fine for tables but views have prefixed attribute "store:Schema" so are not found
                var schemaAttribute = entitySet.Attributes("Schema").FirstOrDefault();
                if (schemaAttribute != null)
                {
                    schemaAttribute.SetValue(schema);
                }
    
                // EF storage xml species a prefix on view Schema attribute as "store:Schema" so are not found
                // here we try various ways to get any "store:Scehma" attributes so far unsuccessfully
                // the most obvious incantaion throws an XmlException as mentioned above
                schemaAttribute = entitySet.Attributes("store:Schema").FirstOrDefault();
                // the next three run fine but do not filter out the needed attribute
                //schemaAttribute = entitySet.Attributes(XName.Get("store", "Schema")).FirstOrDefault();
                //schemaAttribute = entitySet.Attributes(XName.Get("Schema", "store")).FirstOrDefault();
                //XNamespace ns = "store";
                //schemaAttribute = entitySet.Attributes(ns + "Schema").FirstOrDefault();
                if (schemaAttribute != null)
                {
                    // I never hit this though can see the desired XElement w/ a "store:Schema" attribute
                    // I need to reset this schema // does it need to be set as store: prefixed?
                    schemaAttribute.SetValue(schema);
                }
            }

    Dave

    Tuesday, July 29, 2014 3:20 AM

Answers

  • Changing the code to use the NameSpace prefix like this works.

                XNamespace store = "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator";
                schemaAttribute = entitySet.Attributes(store + "Schema").FirstOrDefault();
                if (schemaAttribute != null)
                {
                    schemaAttribute.SetValue(schema);
                }

    Dave


    Tuesday, July 29, 2014 4:00 AM
  • Hello,

    >> Can anyone verify if this is using reflection or the namespace URI to determine the prefix is "store"?

    It is the namespace URI. When loading file, it would initialize the Name property of every element and assign the name and namespace URL to the two properties of XName class: Name and NameSpaceName.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 31, 2014 7:14 AM
    Moderator

All replies

  • Changing the code to use the NameSpace prefix like this works.

                XNamespace store = "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator";
                schemaAttribute = entitySet.Attributes(store + "Schema").FirstOrDefault();
                if (schemaAttribute != null)
                {
                    schemaAttribute.SetValue(schema);
                }

    Dave


    Tuesday, July 29, 2014 4:00 AM
  • I'm glad to hear that you got it working.

    Thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 30, 2014 5:21 AM
    Moderator
  • Can anyone verify if this is using reflection or the namespace URI to determine the prefix is "store"?

    Dave

    Wednesday, July 30, 2014 12:04 PM
  • Hello,

    >> Can anyone verify if this is using reflection or the namespace URI to determine the prefix is "store"?

    It is the namespace URI. When loading file, it would initialize the Name property of every element and assign the name and namespace URL to the two properties of XName class: Name and NameSpaceName.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 31, 2014 7:14 AM
    Moderator
  • Thx!

    Dave

    Thursday, July 31, 2014 12:42 PM