从动态数据站点导出到 XML
我必须更新基于实体框架之上的 DDS 的现有站点,并且它使用来自三个不同数据库的三种不同数据库模型。它需要的是对 ListDetails 页面的简单添加:导出到 XML 按钮...
添加按钮很容易。创建 XML 也不困难。挑战在于获取正确的表进行导出、遍历所有记录和字段并根据系统中 100 多个表中的任何一个生成 XML!
因此,我的按钮调用一个导出 (ashx) 处理程序,该处理程序根据通过参数接收的表名称生成 XML。我的代码是这样的:
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);
不幸的是,这只适用于单个上下文,而我碰巧有三个上下文。 (但这一个是主要上下文。)(此外,可以使用第二个参数来确定正确的上下文。)尽管它允许我确定所需的实体类型,但它仍然无法让我访问其记录和领域。
那么,如何获取该实体的数据呢? (过滤器并不重要;导出将返回所有数据。)
不,没有 EF4。没有.NET 4。这是一个较旧的VS2008项目,不能修改太多,也不能升级...
基本上,查询字符串包含两个参数:ContextID和QueryID。 ContextID 告诉我要使用哪个上下文,并且由于我只有三个不同的上下文,因此一个简单的 switch 命令可以为我解决这个问题。 但其中一个上下文包含 60 多个查询,每个查询都与一个数据库表相关。动态数据站点为我提供了在此表中添加、编辑和删除记录的选项,但它需要以我的自定义代码指定的格式导出。但是对于 60 个表,为每个查询编写一个 switch 语句太多了,所以我需要更通用的东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我让事情变得比需要的更复杂......我从参数开始:
然后我需要确定要使用的正确上下文,这很简单:
接下来我需要在请求的表中获取正确的数据。也很简单:
不知道为什么我试图使该部分比需要的更复杂。
剩下的就是遍历所有字段,但我有一个反射解决方法:
Blah 部分检查 Prop.Name 以查看它是否是实体键、实体状态、子集合或引用,或者它是否只是一个数据字段。这使我能够生成实用的 XML 输出。
As it turns out, I made things more complex than needed... I start with the parameters:
Then I need to determine the right context to use, which is easy:
Next I need to get the proper data in the requested table. Also easy:
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:
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.