我试图在C#中使用实体框架构建一个动态查询。最终目标是允许用户在我的应用程序中构建自己的自定义查询/报告。我从Angular和Typescript获取这些信息,并且我的应用程序两边都有匹配的实体(前端和后端)。我的目标是建立一个sql查询字符串,以后我可以直接在数据库上执行。我目前不关心验证,所以目前,格式错误的查询是有效的结果(我将在稍后解决这个问题 - 一次一个步骤)。我的实体如下:动态查询生成
public class AdHocLine
{
//The table that we are selecting a column from
[JsonProperty(PropertyName = "table")]
public string table;
//The column that a user wants to appear in their report
[JsonProperty(PropertyName = "column")]
public string column;
//Any filter criteria (e.g. 'equals abcd', this would be the 'abcd')
[JsonProperty(PropertyName = "userCriteria")]
public string userCriteria;
//The operator being used (e.g. 'equals abcd', this would be 'equals'
[JsonProperty(PropertyName = "op")]
public string op;
//If we are joining another column, this is the table we would be joining
[JsonProperty(PropertyName = "joinTable")]
public string joinTable;
//If we are joining another column, this is the column we would be joining
[JsonProperty(PropertyName = "joinColumn")]
public string joinColumn;
//Since this entity is meant to be delivered in an array, this is the array index
[JsonProperty(PropertyName = "index")]
public int index;
//This is the column from our current table that we are joining on
[JsonProperty(PropertyName = "joinWith")]
public string joinWith;
}
有什么,我缺少在我的实体,似乎明显的?
在审阅相关问题后,用例可能会有帮助。假设我想从Project表中选择ProjectName(一个字符串),并且希望从其父项ProgramElement(其中Project有一个指向ProgramElement的GUID,名为ProgramElementId)中包含Description列。结果应该如下所示:
SELECT t1.ProjectName, t2.Description FROM Project as t1, ProgramElement as t2 <additional where clause information belongs here>
在此先感谢!
想我要指出的是,用户确定的广告-hoc查询几乎总是非常糟糕的主意。基本上,您的用户可以通过形式不佳的查询来使系统崩溃。事先确定需要什么以及使用参数化存储过程或“预先存储的”查询来访问数据是一个更为安全的选择。虽然需要更多时间和深思熟虑,但“罐装”报告要安全得多,而且几乎总是比临时性更高。话虽如此,我没有看到你的课堂上有什么明显的缺失。 – Kevin
谢谢!我允许AdHoc查询的唯一原因是因为它是在我的需求文档中指定的,这是客户特别要求的。客户习惯于拥有直接的数据库访问权限,所以我试图削减这个选择和报告生成。由于我们仍然处于预发布阶段,因此如果它仍然有点粗糙或缓慢就没关系。这些是我们以后可以清理的东西。不过,你说得好一点,我可能会与我的团队主管谈论如何试图说服客户说'Canned'报告可能会更好。 –
@AndrewM。你到底在问什么?如果不知道您的结果查询的详细程度如何,我们无法告诉您是否缺少某些内容。他们需要能够离开,正确,交叉连接吗? Applys?调用函数或存储过程?声明变量?创建临时表?使用'CASE'语句?因为目前所有这些都缺乏。 – iamdave