我正在尝试创建一个应用程序,其中允许开发人员和最终用户从关系数据库(在运行时选择)检索数据,而无需编写任何 SQL 代码(并且在与数据库交互时最好没有字符串连接)。
该应用程序的目的有两个:表达式树构建器的 GUI 以及由它生成的树的 XML 序列化。
我想请求一些关于如何让事情顺利进行的指导或技巧。我不想让别人为我编写这个 - 这是我的项目,我将是编写代码的人。
所以我的问题是:
-
是否可以通过在运行时仅使用连接字符串来创建对象模型并寻址集合中的数据库元素 - 例如所有表、视图、函数的列表和存储过程?
-
可以在扩展方法中使用上述列表的元素,以便我可以构建表达式树吗?
- Workspace 类和 ItemCollections 从仅包含 CSDL、SSDL 和 MDL 文件(但没有实体类)的数据库中检索这些列表吗?
-
我可以使用 LINQ to XML 来序列化表达式树的结果,还是必须使用特定于实体框架的其他序列化技术?
(就像此处所示的示例:http://msdn.microsoft.com/ en-us/library/bb738528.aspx)
PS:以下是我迄今为止的研究的一些注释:
1. 例如,当 DataContext 仅接收到连接字符串时,DataContext.GetTable() 方法(是的,弱类型方法,而不是通用方法)未检索任何数据。
-
我正在调用 EdmGen 工具来生成架构工件,到目前为止我已经阅读了它们的属性,但我仍在尝试获取某种可在 lambda 表达式中使用的元素列表(表例如)?
-
如果我理解正确的话,在实体框架中,我可以使用元数据工作区和 ItemCollections 等类以及 EdmGen 工具来创建必要的对象集,用这些对象将概念模式表示为对象模型中的对象模型。 如果我理解正确的话
我已经在此处和 MSDN 上浏览了实体框架编程指南以及其他相关主题,但到目前为止我还没有找到与此场景相关的任何信息。
昨天我拿到了《编程实体框架》这本书,希望能在里面找到一些答案。如果我找到任何线索,我会更新这个问题。
因此,在困惑和仍在寻找的情况下,我提前感谢您就此事提供的任何帮助。
此致,
鲍里斯拉夫
I'm trying to make an application in which both the developer and the end user are allowed retrieve data from a relational DB (chosen at runtime) without writing any SQL code at all (and preferably no string concatenations when interacting with the database).
The purpose of the application is to two things: a GUI to an expression tree builder and XML serialization of trees generated by it.
I'd like to ask for some guidance or tips as to how to get things rolling. I don't want someone else to write this for me - this is my project I'll be the one writing the code.
So my questions are:
-
Is it possible by only using only a connection string at run time to be able to create the object model and address the DB's elements in sets - for example a list of all of the tables, views, functions and sprocs?
-
Can the elements of the aforementioned lists be used in the extension methods so I can build an expression tree?
-
Can I use the Metadata Workspace class and the ItemCollections in order to retrieve those lists from the database having only the CSDL, SSDL and MDL files (but no Entity classes)?
-
Can I use LINQ to XML in order to serialize the results of the expression tree or I will have to use some other serialization technology, specific to the Entity Framework?
(Like in the example shown here: http://msdn.microsoft.com/en-us/library/bb738528.aspx)
PS: Here are some notes from my research so far:
1. For example the DataContext.GetTable() method (yes, the weakly typed one, not the generic) did not retrieve any data when the DataContext received only a connection string.
-
I'm calling the EdmGen tool in order to generate the schema artifacts and so far I've read their attributes but I'm still trying to get some sort of list of elements which can be used in a lambda expression (tables for example)?
-
If I understood correctly, in the Entity Framework I can use classes such as the Metadata Workspace and ItemCollections along with the EdmGen tool in order to create the necessary set of objects with which to represent the conceptual schema as an object model in the application.
I've browsed the Entity Framework Programming guide along the other related topics both here and on MSDN but so far I haven't found any information that is relevant to this scenario.
Yesterday I got the "Programming Entity Framework" book and I hope that some of the answers lie in it. I'll update this question if I find any clues.
So, confused and still searching, I thank you in advance for any help on the matter.
Best regards,
Borislav
发布评论
评论(2)
再次非常感谢戴夫发表了他对此事的想法。
以下是我迄今为止的研究总结:
我将放弃 LINQ to Entities,转而使用 LINQ to SQL - 看起来它会让我更轻松地完成这个项目。
以下是我的问题以及到目前为止我得到的答案和评论:
@Dave - 实际上这是可能的 - 我查看了 LINQPad 官方网站的“LINQPad 如何工作”部分,答案是:使用 Reflection.Emit 在运行时生成类以创建方法的内存占用,属性及其属性,然后基于它们创建类。另外,我从 MSDN 上的版主那里得到了这个提示: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/471c8672-0a77-4186-b1d3-844abd8a822b
因此,在某种程度上,我认为可以在运行时检索对象模型。
我想我在这里没有问正确的问题 - 我想说的是:
在运行时,您会发现有一个表 Table,其中包含列 Col1、Col2 和 Col3。在一个简单的 LINQ to SQL 应用程序中,代码将是
但是我正在寻找这个: 用户选择要参与选择的列,因此最终的选择将是这样的:
但是,这是无法完成的,因为要找出列的名称还不够 - 了解列的类型才能完成工作。我现在正在研究这个问题,但只是在 LINQ to SQL 方面,而不是 LINQ to Entities - 那里的逻辑可能不同。
@Dave:这是一个项目场景:
用户想要获取以下数据的 XML:
Orders.Name、Order.Price、Orders.Products.Name、Orders.Products.Manufacturer 其中 Orders.OrderID = 1234。
输出应该是这样的:
由于我已经在这个问题上添加了 EF 标签,因此我将关闭它现在我正在寻找 LINQ to SQL 答案,没有 EF。
如果我询问有关此主题的其他问题,我将尝试为其附加适当的 LINQ to SQL 标记。
祝一切顺利,鲍里斯拉夫。
Once again big thanks to dave for posting his thoughts on the matter.
Here's a summary of my research so far:
I'm abandoning LINQ to Entities in favor to LINQ to SQL - it looks like it will give me an easier time with this project.
So here are again my questions and the answers and comments I have till now:
@Dave - actually this is sort of possible - I took a look at the "How LINQPad Work" section of LINQPad's official website and the answer was: Generate the classes at runtime using Reflection.Emit to create an in-memory footprint of methods, properties and their attributes and then create the classes based on them. Also, I go this tip from a moderator on MSDN: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/471c8672-0a77-4186-b1d3-844abd8a822b
So in a way I think it's possible to retrieve the Object Model at run time.
I guess I didnt' ask the right question here - what I meant to say was:
At run time you are aware that there is a table Table with columns Col1, Col2 and Col3. In a simple LINQ to SQL app the code would be
However I'm looking for this: The user selects which columns to participate in the select so the final select would be something like this:
However this cannot be done because finding out a column's name is not sufficient - knowing the column's type is what can get the job done. I'm looking into this right now but only in terms of LINQ to SQL, not LINQ to Entities - the logic may be different there.
@Dave: Here's a scenario for project:
The user wants to get an XML on the following data:
Orders.Name, Order.Price, Orders.Products.Name, Orders.Products.Manufacturer Where Orders.OrderID = 1234.
Here's what the output should be:
Since I've already put EF tags on this question I'll close it since now I'm looking for LINQ to SQL answers, no EF.
If I ask other quetions about this subject, I'll try to attach the approptiate LINQ to SQL tags to it.
All the best, Borislav.
这些问题重理论轻应用,让人很难理解你想要完成的任务。根据我从您的问题中了解到的内容,这是我的最佳答案。如果您愿意更新一些有关您的要求的详细信息,我可以对其进行改进。
1:这听起来像“我的实体模型可以在运行时使用连接字符串自动生成以包含我的所有数据库对象吗?”:
不。您的实体模型由实体数据模型定义(即
MyModel.edmx。) 这用于生成对象服务类
MyModelEntities。
该模型必须存在且可用,以便实体框架提供程序对其执行查询。2:如果“表达式树”指的是 lambda 或 LINQ 语法:
任何实现 IEnumerable、IQueryable 或其他类似接口的集合对象都可以使用 LINQ 进行查询(
from i in EntitySet... select i
) 或 lambda (.Where(x=>x.Id == 5)
) 语法。3:如果你的意思是,你可以使用EDM定义(CSDL,SSDL,MDL)本身来直接查询数据库:
这就是LINQ to Entities的作用:将LINQ查询转换为SQL查询。您为什么要重塑 L2E 提供商?
4:实体对象序列化在 EF v1 中有点奇怪(读:损坏)。 Entity 对象带有大量 EF 包袱(它们派生自 EntityObject 并带有额外的属性),因此序列化可能很困难。它对您的效果如何取决于您的实体对象以及序列化后您对它们执行的操作。
These questions are heavy on theory and light on application, making it difficult to understand what you're trying to accomplish. Here's my best answers based on what I can understand from your questions. If you'll update with some detail on what your requirements are, I can refine them.
1: This sounds like "can my Entity Model be autogenerated at runtime with a connectionstring to include all my database objects?":
No. Your Entity Model is defined by your Entity Data Model (i.e.
MyModel.edmx.
) This is used to generate the Object Services class,MyModelEntities.
The model must exist and be available in order for the Entity Framework Provider to execute queries against it.2: If by "expression tree" you mean lambda or LINQ syntax:
Any collection object that implements IEnumerable, IQueryable, or other interfaces like these can be queried using LINQ (
from i in EntitySet... select i
) or lambda (.Where(x=>x.Id == 5)
) syntaxes.3: If you mean, can you use the EDM definition (CSDL, SSDL, MDL) by itself to query the database directly:
This is what LINQ to Entities does: translates LINQ queries into SQL queries. Why would you want to reinvent the L2E Provider?
4: Entity object serialization is a little weird (read:broken) in EF v1. The Entity objects come with a lot of EF baggage (they derive from EntityObject and carry extra properties) so serialization can be difficult. How well it works for you will depend on your entity objects and what you do with them once they're serialized.