Export to XML from Dynamic Data Site
Asked Answered
Q

1

6

I have to update an existing site that's based on DDS on top of the Entity Framework, and it uses three different database models from three different databases. And what it needs is a simple addition to the ListDetails page: an export to XML button...
Adding the button is easy. Creating the XML isn't difficult either. The challenge is getting the proper table to export, walk through all records and fields and generate the XML based on any of the 100+ tables in the system!
So, my button calls an export (ashx) handler which generates the XML based on the table who'se name it receives through it's parameters. The code I have is something like this:

        Content_CobaEntities Coba = new Content_CobaEntities();
        MetadataWorkspace metadataWorkspace = Coba.MetadataWorkspace;
        EntityContainer container = metadataWorkspace.GetItems<EntityContainer>(DataSpace.CSpace).First();
        string namespaceName = metadataWorkspace.GetItems<EntityType>(DataSpace.CSpace).First().NamespaceName;
        EntitySetBase entitySetBase = container.BaseEntitySets.FirstOrDefault(set => set.ElementType.Name == Entity);

Unfortunately, this only works with a single context and I happen to have three. (But this one is the main context.) (Besides, a second parameter could be used to determine the proper context.) And although it allows me to determine the entity type that's needed, it still won't provide me access to it's records and fields.

So, how to I get the data for this entity? (Filters aren't important; the export will return all data.)

And no, no EF4. No .NET 4. This is an older VS2008 project and cannot be modified to much, nor can it be upgraded...


Basically, the querystring contains two parameters: ContextID and QueryID. The ContextID tells me which context to use and since I only have three different contexts, a simple switch-command solves this for me. But one of the contexts contains 60+ queries, each of them related to a single database table. The Dynamic Data Site provides me with options to add, edit and delete records from this table but it needs to be exported in a format dictated by my custom code. But with 60 tables, it's too much to write a switch statement for each and every query so I need something more generic.

Quodlibet answered 27/6, 2011 at 7:11 Comment(2)
Are you saying that you can't connect to another EF context other than the main context?Sagerman
Actually, the export handler must also be available from other locations and users need to be able to bookmark it. I can get the proper context but now I need to loop through the proper query, with all fields and values to generate the custom XML.Quodlibet
Q
2

As it turns out, I made things more complex than needed... I start with the parameters:

        string Entity = context.Request.QueryString.Get("Entity");
        string ContextID = context.Request.QueryString.Get("Context");

Then I need to determine the right context to use, which is easy:

        ObjectContext Context;
        if (ContextID.Contains("Content_LogEntities")) { Context = new Content_LogEntities(); }
        else if (ContextID.Contains("Content_CobusEntities")) { Context = new Content_CobusEntities(); }
        else { Context = new Content_CobaEntities(); };

Next I need to get the proper data in the requested table. Also easy:

        ObjectQuery Tabel = Context.CreateQuery<EntityObject>(string.Format("[{0}]", Entity));

Don't know why I was trying to make that part more complex than needed.

What's left is walking through all fields, but I have a reflection work-around:

        foreach (var rec in Tabel)
        {
            foreach (PropertyInfo Prop in rec.GetType().GetProperties())
            {
                // Blah
            }
        }

The Blah-part checks the Prop.Name To see if it's an entity key, entity state, child collection or reference or if it's just a data-field. This allows me to generate a practical XML output.

Quodlibet answered 30/6, 2011 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.