Export to XML from a dynamic data site

I need to upgrade an existing DDS-based site on top of the Entity Framework and it uses three different database models from three different databases. And what he needs is a simple addition to the ListDetails page: an XML export button ...
Adding a button is very simple. Creating XML is also not difficult. The task is to get the right table for export, go through all the records and fields and create XML based on any of the 100+ tables in the system!
So, my button calls the export handler (ashx), which generates XML based on the who'se name table that it gets through it. The code I have looks 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 one context, and I have three. (But this is the main context.) (In addition, you can use the second parameter to determine the correct context.) And although it allows me to determine the type of entity that you need, it still will not give me access to them. and fields.

So how do I get the data for this object? (Filters are not important; export will return all data.)

And no, no EF4. No .NET 4. This is an older project, VS2008 and cannot be changed to a large number, and it cannot be updated ...


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

+6
source share
1 answer

As it turned out, I made things more complicated than necessary ... I start with the options:

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

Then I need to determine the correct 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 correct data in the requested table. Also easy:

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

I don’t know why I tried to make this part more difficult than necessary.

What remains is to go through all the fields, but I have a reflection:

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

The Blah part checks the name Prop.Name. To find out if this is an entity key, entity, child collection or reference, or if it is just a data field. This allows me to generate practical XML output.

+2
source

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


All Articles