具有 SQL 支持存储的 OData UnTyped 提供程序
我在数据库中有一个表,它存储键/值对,其中包含有关值字段中存储的数据类型的一些类型信息。
我已经阅读了有关创建非类型化数据提供程序的博客系列,并抓取并弄乱了 odata 中的示例,但我不知道如何将使用内存字典作为后备存储的示例转换为使用EF 或 Linq to Sql 实体作为数据的实际存储。
I have a table in a database which is a store of Key/Value pairs, with some type information on the type of data stored in the value field.
I've read the blog series on creating an untyped data provider, and have grabbed and messed with the samples from odata, but I can not figure out how to turn the sample which uses an in-memory Dictionary as it's backing store, to use either EF or Linq to Sql entities as the actual store of data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你问的这个问题比较难。示例使用 Dictionary 的原因是为了避免编写自定义 LINQ 提供程序(IQueryable 实现)。就您而言,这几乎就是您需要做的。
此外,EF 和 LINQ to SQL 似乎都不适合您,因为两者都假设统计数据模型,其中实体是表中的行,属性是该表的列(粗略地说)。你的情况似乎并非如此。如果我理解正确的话,你的模型中每个属性都有一行。
为了实现这项工作,您必须至少部分实现 IQueryable 并使其理解您的数据结构。例如,过滤是不同的,通常名称属性上的简单过滤器可以在数据库中表示为 WHERE Name='...',但在您的情况下,这可能会转换为需要搜索键/值表的联接查找具有名称的行,然后比较其值。
您可以使用上面建议的方法,将所有内容加载到内存中。编码相当简单,但它将所有内容加载到内存中。
或者您可以尝试编写自定义 LINQ 提供程序。这很难。但如果您想尝试一下,我建议您看一下这个博客系列,它基本上描述了如何实现 LINQ to SQL 之类的东西:链接
我正在编写一个系列,描述您提供的 eof 表达式需要支持哪些类型,才能与 WCF 数据服务配合使用。这也可以派上用场: 链接
请注意,您可以自己只编写提供程序的部分内容。例如,您可以自己处理过滤,并将复杂的投影/扩展留给 LINQ to Objects。这种方法可以使您的提供商更容易实施。
What you're asking is rather hard. The reason the samples use Dictionary is to avoid writing custom LINQ provider (IQueryable implementation). In your case that's pretty much what you need to do.
In addition it seems that neither EF nor LINQ to SQL will work for you because both assume statis data models where entities are rows in a table and properties are columns of that table (roughly speaking). That doesn't seem to be the case for you. If I understand it correctly you have a row per property in your model.
To make this work you would have to implement at least partially IQueryable and make it understand your data structure. For example filtering is different, typically a simple filter on a Name property can be expressed in DB as a WHERE Name='...', but in your case this probably translates into a join where you need to search to key/value table for the row with the Name and then compare its value.
You can either use the approach you suggested above, which loads everything into memory. It's rather simple to code, but it loads everything into memory.
Or you can try writing a custom LINQ provider. This is very hard. But if you want to try it anway, I would suggest you take a look at this blog series which basically describes how something like LINQ to SQL could be implemented: Link
I'm writing a series which describes what typ eof expressions you provider needs to support in order for it to work with WCF Data Services. That can come handy as well: Link
Note that it is possible to write only portions of the provider yourself. You could for example deal with filtering yourself and leave the complex projections/expansions up to LINQ to Objects. This approach can make your provider a bit easier to implement.
好的,这是我到目前为止所拥有的。唯一的问题是所有查询都必须在内存中执行:
Ok, here is what I have so far. The only issue is that all the query has to be performed in memory: