简体   繁体   English

用于在SQL Server中查询JSON字符串的实体框架

[英]Entity Framework for querying JSON strings in SQL Server

I'm looking for anyone who's done anything along the lines of querying JSON strings with the Entity Framework. 我正在寻找任何在实体框架中查询JSON字符串的人。

I should give a little background about what I'm trying to do here. 我应该给出一些关于我在这里要做什么的背景知识。 The database I'm using is for a workflow engine that I'm working on. 我正在使用的数据库是我正在处理的工作流引擎。 It handles all the workflow data, and also allows you to store some custom data as a JSON string. 它处理所有工作流数据,还允许您将一些自定义数据存储为JSON字符串。 The workflow engine I'm using handles the serializing and de-serializing of the JSON strings on a per request basis, but in the event I would want to do a query and filter based on values in the JSON string, I would have to pull the entire table into memory and de-serialize all the entries and then filter. 我正在使用的工作流引擎在每个请求的基础上处理JSON字符串的序列化和反序列化,但是如果我想要根据JSON字符串中的值进行查询和过滤,我将不得不拉整个表进入内存并反序列化所有条目然后过滤。 This is, for obvious reasons, not acceptable. 出于显而易见的原因,这是不可接受的。 The reason for doing this, is we want a single workflow database that can be used for all applications that use this workflow engine, and we are trying to avoid having to do cross database views to seperate application specific databases to get the custom data. 这样做的原因是,我们想要一个可用于所有使用此工作流引擎的应用程序的工作流数据库,并且我们正在尝试避免必须跨数据库视图来分离特定于应用程序的数据库以获取自定义数据。 Since in most cases, the custom request data that is being stored as JSON strings is relatively simple, and in most cases isn't needed when querying, which is by design. 因为在大多数情况下,作为JSON字符串存储的自定义请求数据相对简单,并且在查询时大多数情况下不需要,这是设计的。 But in the event that we do need to do custom searches, we need a way to be able to parse these custom JSON objects. 但是,如果我们确实需要进行自定义搜索,我们需要一种方法来解析这些自定义JSON对象。 And I'm hoping this can be done dynamically with Entity so I don't have to write extra stored procs for querying specific types of objects. 我希望这可以用Entity动态完成,所以我不必编写额外的存储过程来查询特定类型的对象。 Ideally I would just have one library that uses entity to allow querying of any JSON data structure. 理想情况下,我只有一个库使用实体来允许查询任何JSON数据结构。

I started with a database function that I found that parses JSON and returns a flattened table that contains the values (parent object id, name, value, and type). 我从一个数据库函数开始,我发现它解析了JSON并返回一个包含值(父对象id,名称,值和类型)的扁平表。 Then imported that function into my entity model. 然后将该函数导入我的实体模型。 Here's a link to where I got the code from. 这是我从中获取代码的链接。 Pretty interesting article. 非常有趣的文章。

Consuming JSON Strings in SQL Server 在SQL Server中使用JSON字符串

Here's the basics of where I'm at. 这是我所处的基础知识。

using (var db = new WorkflowEntities()) {
    var objects = db.Requests.RequestData();
}

In the above code example, Request object is my base workflow Request object. 在上面的代码示例中,Request对象是我的基本工作流Request对象。 RequestData() is an extension method on type RequestData()是类型的扩展方法

DbSet<Request>

and parseJSON is the name of my database function. 和parseJSON是我的数据库函数的名称。

My plan is to write a series of extension methods that will filter the Queryables 我的计划是编写一系列将过滤Queryables的扩展方法

IQueryable<parseJSON_result>

So for example, if I have an object that looks like this. 例如,如果我有一个看起来像这样的对象。

RequestDetail : {
    RequestNumber: '123',
    RequestType: 1,
    CustomerId: 1
}

I would be able to do something like 我可以做类似的事情

db.Request.RequestData().Where("RequestType", 1);

or something along those lines. 或类似的规定。 The .Where method would Take RequestData(), which is an IQueryable that contains the parsed JSON, it would filter and return the new IQueryable result. .Where方法将Take RequestData(),它是一个包含已解析JSON的IQueryable,它将过滤并返回新的IQueryable结果。

So my question really is, has anyone done anything like this? 所以我的问题是,有人做过这样的事吗? If so, what kind of approach have you taken? 如果是这样,你采取了什么样的方法? My original intent was to do something dictionary style, but it seemed too difficult. 我最初的意图是做一些字典风格,但似乎太难了。 Any thoughts, ideas, suggestions, wisdom, would be much appreciated. 任何想法,想法,建议,智慧,将不胜感激。 I worked on this for awhile, and I feel like I really didn't get that far. 我在这方面工作了一段时间,我觉得我真的没有那么远。 Which is mostly just because I can't decide how I want the syntax to look, and I'm not sure if I should be doing more work database side. 这主要是因为我无法决定我希望语法看起来如何,而且我不确定我是否应该在数据库方面做更多工作。

This was my original idea for syntax, but I couldn't run the [] operator without hydrating the object. 这是我对语法的最初想法,但是如果不对对象进行保湿,我就无法运行[]运算符。

db.Request.Where(req => req.RequestData()["RequestType"] == 1).Select(req => req.RequestData()["CustomerInfo"]);

I know this is a pretty long post, so if you've read this far, thanks for just taking the time to read the whole thing. 我知道这是一个相当长的帖子,所以如果你已经阅读了这篇文章,感谢你花时间阅读整篇文章。

As of SQL Server 2016, FOR JSON and OPENJSON exist, equivalent to FOR XML and OPENXML. 从SQL Server 2016开始,存在FOR JSON和OPENJSON,相当于FOR XML和OPENXML。 You can Index on expressions that reference JSON stored in NVARCHAR columns. 您可以索引引用存储在NVARCHAR列中的JSON的表达式。

This is a very late answer, but for anyone who is still searching... 这是一个非常晚的答案,但对于仍在搜索的人...

As @Emyr says, SQL 2016 supports querying inside JSON columns using JSON_VALUE or OPENJSON statements. 正如@Emyr所说,SQL 2016支持使用JSON_VALUE或OPENJSON语句查询JSON列内部。

Entity Framework still does not support this directly, but you can use the SqlQuery method to run a raw SQL command directly against the database which can query inside JSON columns and saves querying and deserializing every row in order to run a simple query. 实体框架仍然不直接支持这一点,但您可以使用SqlQuery方法直接针对数据库运行原始SQL命令,该数据库可以在JSON列内查询并保存查询和反序列化每一行以运行简单查询。

What you could do is create a CLR SQL Server User-Defined Function then use it from your query. 您可以做的是创建一个CLR SQL Server用户定义函数,然后在查询中使用它。

See this link https://msdn.microsoft.com/en-us/library/ms131077.aspx 请参阅此链接https://msdn.microsoft.com/en-us/library/ms131077.aspx

i would think that a table-valued functions is more suited for your situation. 我认为表值函数更适合您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM